

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Recursion may have the following definitions: -The nested repetition of identical algorithm is recursion. -It is a technique of defining an object/process by itself. -Recursion is a process by which a function calls itself repeatedly until some specified condition has been satisfied. When to use recursion Recursion can be used for repetitive computations in which each action is stated in terms of previous result. There are two conditions that must be satisfied by any recursive procedure.
In making the decision about whether to write an algorithm in recursive or non- recursive form, it is always advisable to consider a tree structure for the problem. If the structure is simple then use non-recursive form. If the tree appears quite bushy, with little duplication of tasks, then recursion is suitable. The recursion algorithm for finding the factorial of a number is given below, Algorithm : factorial-recursion Input : n, the number whose factorial is to be found. Output : f, the factorial of n Method : if(n=0) f= else f=factorial(n-1) * n if end algorithm ends. The general procedure for any recursive algorithm is as follows,
2.4.2 Iteration v/s Recursion Demerits of recursive algorithms
Recursion is a top down approach to problem solving. It divides the problem into pieces or selects out one key step, postponing the rest. Iteration is more of a bottom up approach. It begins with what is known and from this constructs the solution step by step. The iterative function obviously uses time that is O(n) where as recursive function has an exponential time complexity. It is always true that recursion can be replaced by iteration and stacks. It is also true that stack can be replaced by a recursive program with no stack.
directly, and
problem,
A function that calls itself is an important feature of C and such functions are called
recursus, which means, “to run back”. “Recursive” thinking may be tough for beginners. In this chapter, I have presented some interesting recursive programs. Few programs are my original work, others are improved version of existing recursive programs.