What Is Recursion
What Is Recursion
Step1 - Define a base case: Identify the simplest case for which
the solution is known or trivial. This is the stopping condition
for the recursion, as it prevents the function from infinitely
calling itself.
int fact(int n)
{
if (n < = 1) // base case
return 1;
else
return n*fact(n-1);
}
In the above example, the base case for n < = 1 is defined and the larger
value of a number can be solved by converting to a smaller one till the base
case is reached.
How a particular problem is solved using recursion?
The idea is to represent a problem in terms of one or more smaller problems,
and add one or more base conditions that stop the recursion. For example,
we compute factorial n if we know the factorial of (n-1). The base case for
factorial would be n = 0. We return 1 when n = 0.
Why Stack Overflow error occurs in recursion?
If the base case is not reached or not defined, then the stack overflow
problem may arise. Let us take an example to understand this.
int fact(int n)
{
// wrong base case (it may cause
// stack overflow).
if (n == 100)
return 1;
else
return n*fact(n-1);
}
If fact(10) is called, it will call fact(9), fact(8), fact(7), and so on but the
number will never reach 100. So, the base case is not reached. If the
memory is exhausted by these functions on the stack, it will cause a stack
overflow error.
What is the difference between direct and indirect recursion?
A function fun is called direct recursive if it calls the same function fun. A
function fun is called indirect recursive if it calls another function say fun_new
and fun_new calls fun directly or indirectly. The difference between direct
and indirect recursion has been illustrated in Table 1.
// An example of direct recursion
void directRecFun()
{
// Some code....
directRecFun();
// Some code...
}
indirectRecFun2();
// Some code...
}
void indirectRecFun2()
{
// Some code...
indirectRecFun1();
// Some code...
}
What is the difference between tailed and non-tailed recursion?
A recursive function is tail recursive when a recursive call is the last thing
executed by the function. Please refer tail recursion article for details.
How memory is allocated to different function calls in recursion?
When any function is called from main(), the memory is allocated to it on the
stack. A recursive function calls itself, the memory for a called function is
allocated on top of memory allocated to the calling function and a different
copy of local variables is created for each function call. When the base case
is reached, the function returns its value to the function by whom it is called
and memory is de-allocated and the process continues.
Let us take the example of how recursion works by taking a simple function.
Recursion VS Iteration
SR
Recursion Iteration
No.
Terminates when the base case becomes Terminates when the condition
1)
true. becomes false.
Every recursive call needs extra space in Every iteration does not require
3)
the stack memory. any extra space.