![]() |
VOOZH | about |
The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function.
Steps to Implement Recursion
Step1 - Define a base case: Identify the simplest (or base) 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.
Step2 - Define a recursive case: Define the problem in terms of smaller subproblems. Break the problem down into smaller versions of itself, and call the function recursively to solve each subproblem.
Step3 - Ensure the recursion terminates: Make sure that the recursive function eventually reaches the base case, and does not enter an infinite loop.
Step4 - Combine the solutions: Combine the solutions of the subproblems to solve the original problem.
Input : n = 3
Output : 6
Explanation : The sum of first 3 natural numbers is 1+2+3 = 6.Input : n = 7
Output : 28
Explanation : The sum of first 7 natural numbers is 1+2+3+4+5+6+7 = 28.
15
Execution Flow of the Recursive Solution
Calls are stacked as sum(3) → sum(2) → sum(1) before any addition happens. Results are added back in reverse: sum(1) = 1, sum(2) = 3, sum(3) = 6.
Need of Recursion?
What is the base condition in recursion?
A recursive program stops at a base condition. There can be more than one base conditions in a recursion. In the above program, the base condition is when n = 1.
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.
The factorial of a number n (where n >= 0) is the product of all positive integers from 1 to n. To compute the factorial recursively, we calculate the factorial of n by using the factorial of (n-1). The base case for the recursive function is when n = 0, in which case we return 1.
Factorial of 5 : 120
Illustration of the above code:
When does Stack Overflow error occur 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);
}
fact(10) is called, the function will recursively call fact(9), then fact(8), fact(7), and so on. However, the base case checks if n == 100. Since n will never reach 100 during these recursive calls, the base case is never triggered. As a result, the recursion continues indefinitely.if (n == 0) to ensure that the recursion terminates and the function doesn't run out of memory.What is the difference between direct and indirect recursion?
A function is called direct recursive if it calls itself directly during its execution. In other words, the function makes a recursive call to itself within its own body.
An indirect recursive function is one that calls another function, and that other function, in turn, calls the original function either directly or through other functions. This creates a chain of recursive calls involving multiple functions, as opposed to direct recursion, where a function calls itself.
// An example of direct recursion
void directRecFun()
{
// Some code....
directRecFun();
// Some code...
}
// An example of indirect recursion
void indirectRecFun1()
{
// Some code...
indirectRecFun2();
// Some code...
}
void indirectRecFun2()
{
// Some code...
indirectRecFun1();
// Some code...
}
How memory is allocated to different function calls in recursion?
Recursion uses more memory to store data of every recursive call in an internal function call stack.
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.
3 2 1 1 2 3
Initial Call: When printFun(3) is called from main(), memory is allocated for printFun(3). The local variable test is initialized to 3, and statements 1 to 4 are pushed onto the stack.
First Recursive Call:
printFun(3) calls printFun(2).printFun(2) is allocated, the local variable test is initialized to 2, and statements 1 to 4 are pushed onto the stack.Second Recursive Call:
printFun(2) calls printFun(1).printFun(1) is allocated, the local variable test is initialized to 1, and statements 1 to 4 are pushed onto the stack.Third Recursive Call:
printFun(1) calls printFun(0).printFun(0) is allocated, the local variable test is initialized to 0, and statements 1 to 4 are pushed onto the stack.Base Case: When printFun(0) is called, it hits the base case (if statement) and returns control to printFun(1).
Returning from Recursion:
printFun(0), the remaining statements of printFun(1) are executed and it returns control to printFun(2).printFun(2), control returns to printFun(3).Output: As a result, the output will print the values in the following order:
The memory stack grows with each function call and shrinks as the recursion unwinds, following the LIFO structure.
What are the advantages of recursive programming over iterative programming?
What are the disadvantages of recursive programming over iterative programming?
Note every recursive program can be written iteratively and vice versa is also true.
Example 3 : Fibonacci with Recursion
Write a program and recurrence relation to find the Fibonacci series of n where n >= 0.
Mathematical Equation:
n if n == 0, n == 1;
fib(n) = fib(n-1) + fib(n-2) otherwise;
Recurrence Relation:
T(n) = T(n-1) + T(n-2) + O(1)
Fibonacci series of 5 numbers is: 0 1 1 2 3
Recursion Tree for the above Code:
These are just a few examples of the many applications of recursion in computer science and programming. Recursion is a versatile and powerful tool that can be used to solve many different types of problems.
Summary of Recursion:
Output based practice problems for beginners:
Practice Recursion - Easy
Practice Recursion - Medium
Practice Recursion - Hard
Quiz on Recursion