Recursion
Recursion
Courses Login
Write an Article
Menu
Recursion
What is Recursion?
The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called as
recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers
of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc.
int fact(int n)
{
if (n < = 1) // base case
return 1;
else
return n*fact(n-1);
}
In the above example, base case for n < = 1 is de ned and larger value of number can be solved by converting to smaller one till
base case is reached.
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 number will never reach 100. So, the base case is not reached.
If the memory is exhausted by these functions on stack, it will cause stack over ow error.
directRecFun();
// Some code...
}
indirectRecFun2();
// Some code...
}
void indirectRecFun2()
{
// Some code...
indirectRecFun1();
// Some code...
}
Let us take the example how recursion works by taking a simple function.
CPP
// A C++ program to demonstrate working of
// recursion
#include<bits/stdc++.h>
using namespace std;
int main()
{
int test = 3;
printFun(test);
}
Java
Python3
# A Python 3 program to
# demonstrate working of
# recursion
def printFun(test):
test = 3
printFun(test)
C#
// A C# program to demonstrate
// working of recursion
using System;
class GFG {
// function to demonstrate
// working of recursion
static void printFun(int test)
{
if (test < 1)
return;
else
{
Console.Write(test + " ");
// statement 2
printFun(test - 1);
// Driver Code
public static void Main(String[] args)
{
int test = 3;
printFun(test);
}
}
PHP
<?php
// PHP program to demonstrate
// working of recursion
// function to demonstrate
// working of recursion
function printFun($test)
{
if ($test < 1)
return;
else
{
echo("$test ");
// statement 2
printFun($test-1);
echo("$test ");
return;
}
}
// Driver Code
$test = 3;
printFun($test);
Output :
321123
When printFun(3) is called from main(), memory is allocated to printFun(3) and a local variable test is initialized to 3 and
statement 1 to 4 are pushed on the stack as shown in below diagram. It rst prints ‘3’. In statement 2, printFun(2) is called and
memory is allocated to printFun(2) and a local variable test is initialized to 2 and statement 1 to 4 are pushed in the stack.
Similarly, printFun(2) calls printFun(1) and printFun(1) calls printFun(0). printFun(0) goes to if statement and it return to
printFun(1). Remaining statements of printFun(1) are executed and it returns to printFun(2) and so on. In the output, value from 3
to 1 are printed and then 1 to 3 are printed. The memory stack has been shown in below diagram.
Quiz on Recursion
Coding Practice on Recursion:
All Articles on Recursion
Recursive Practice Problems with Solutions
This article is contributed by Sonal Tuteja. If you like GeeksforGeeks and would like to contribute, you can also write an article
using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the
GeeksforGeeks main page and help other Geeks.
Please write comments if you nd anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
Tail Recursion
Sum of digit of a number using recursion
Sum of natural numbers using recursion
Product of 2 numbers using recursion | Set 2
Reversing a queue using recursion
Generating subarrays using recursion
Generating all possible Subsequences using Recursion
Product of 2 Numbers using Recursion
Sort a stack using recursion
Print 1 to 100 in C++, without loop and recursion
Difference between Recursion and Iteration
Practice Questions for Recursion | Set 3
Practice Questions for Recursion | Set 1
Practice Questions for Recursion | Set 5
Reverse a stack using recursion
Improved By : Anshul_Aggarwal
8
To-do Done 2.6
Based on 30 vote(s)
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
Previous
DDA Line generation Algorithm in Computer Graphics
Next
Program to implement Collatz Conjecture
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Load Comments
Find the Initial Array from given array after range sum queries
Ternary Search
Backtracking | Introduction
How will you print numbers from 1 to 100 without using loop? | Set-2
Most visited in Algorithms
Find the repeating and the missing number using two equations
Print all paths from top left to bottom right in a matrix with four moves allowed
COMPANY
About Us
Careers
Privacy Policy
Contact Us
LEARN
Algorithms
Data Structures
Languages
CS Subjects
Video Tutorials
PRACTICE
Company-wise
Topic-wise
Contests
Subjective Questions
CONTRIBUTE
Write an Article
Write Interview Experience
Internships
Videos