# Introduction to Recursion – Data Structure and Algorithm Tutorials

• Difficulty Level : Easy
• Last Updated : 19 Sep, 2022

What is Recursion?
The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using a 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. A recursive function solves a particular problem by calling a copy of itself and solving smaller subproblems of the original problems. Many more recursive calls can be generated as and when required. It is essential to know that we should provide a certain case in order to terminate this recursion process. So we can say that every time the function calls itself with a simpler version of the original problem.

Need of Recursion

Recursion is an amazing technique with the help of which we can reduce the length of our code and make it easier to read and write. It has certain advantages over the iteration technique which will be discussed later. A task that can be defined with its similar subtask, recursion is one of the best solutions for it. For example; The Factorial of a number.

Properties of Recursion:

• Performing the same operations multiple times with different inputs.
• In every step, we try smaller inputs to make the problem smaller.
• Base condition is needed to stop the recursion otherwise infinite loop will occur.

A Mathematical Interpretation

Let us consider a problem that a programmer has to determine the sum of first n natural numbers, there are several ways of doing that but the simplest approach is simply to add the numbers starting from 1 to n. So the function simply looks like this,

approach(1) – Simply adding one by one

f(n) = 1 + 2 + 3 +……..+ n

but there is another mathematical approach of representing this,

approach(2) – Recursive adding

f(n) = 1                  n=1

f(n) = n + f(n-1)    n>1

There is a simple difference between the approach (1) and approach(2) and that is in approach(2) the function “ f( ) ” itself is being called inside the function, so this phenomenon is named recursion, and the function containing recursion is called recursive function, at the end, this is a great tool in the hand of the programmers to code some problems in a lot easier and efficient way.

How are recursive functions stored in memory?

Recursion uses more memory, because the recursive function adds to the stack with each recursive call, and keeps the values there until the call is finished. The recursive function uses LIFO (LAST IN FIRST OUT) Structure just like the stack data structure. https://www.geeksforgeeks.org/stack-data-structure/

What is the base condition in recursion?
In the recursive program, the solution to the base case is provided and the solution to the bigger problem is expressed in terms of smaller problems.

```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...
}

// An example of indirect recursion
void indirectRecFun1()
{
// 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.

## CPP

 `// A C++ program to demonstrate working of` `// recursion` `#include ` `using` `namespace` `std;`   `void` `printFun(``int` `test)` `{` `    ``if` `(test < 1)` `        ``return``;` `    ``else` `{` `        ``cout << test << ``" "``;` `        ``printFun(test - 1); ``// statement 2` `        ``cout << test << ``" "``;` `        ``return``;` `    ``}` `}`   `// Driver Code` `int` `main()` `{` `    ``int` `test = 3;` `    ``printFun(test);` `}`

## Java

 `// A Java program to demonstrate working of` `// recursion` `class` `GFG {` `    ``static` `void` `printFun(``int` `test)` `    ``{` `        ``if` `(test < ``1``)` `            ``return``;` `        ``else` `{` `            ``System.out.printf(``"%d "``, test);` `            ``printFun(test - ``1``); ``// statement 2` `            ``System.out.printf(``"%d "``, test);` `            ``return``;` `        ``}` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `test = ``3``;` `        ``printFun(test);` `    ``}` `}`   `// This code is contributed by` `// Smitha Dinesh Semwal`

## Python3

 `# A Python 3 program to` `# demonstrate working of` `# recursion`     `def` `printFun(test):`   `    ``if` `(test < ``1``):` `        ``return` `    ``else``:`   `        ``print``(test, end``=``" "``)` `        ``printFun(test``-``1``)  ``# statement 2` `        ``print``(test, end``=``" "``)` `        ``return`   `# Driver Code` `test ``=` `3` `printFun(test)`   `# This code is contributed by` `# Smitha Dinesh Semwal`

## 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);`   `            ``Console.Write(test + ``" "``);` `            ``return``;` `        ``}` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `Main(String[] args)` `    ``{` `        ``int` `test = 3;` `        ``printFun(test);` `    ``}` `}`   `// This code is contributed by Anshul Aggarwal.`

## PHP

 ``

## Javascript

 ``

Output :

`3 2 1 1 2 3`

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 first 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 into 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). The remaining statements of printFun(1) are executed and it returns to printFun(2) and so on. In the output, values from 3 to 1 are printed and then 1 to 3 are printed. The memory stack has been shown in below diagram. Recursion VS Iteration

Now, let’s discuss a few practical problems which can be solved by using recursion and understand its basic working. For basic understanding please read the following articles.
Basic understanding of Recursion.
Problem 1: Write a program and recurrence relation to find the Fibonacci series of n where n>2 .
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)`

Recursive program:

```Input: n = 5
Output:
Fibonacci series of 5 numbers is : 0 1 1 2 3```

Implementation:

## C++

 `// C++ code to implement Fibonacci series` `#include ` `using` `namespace` `std;`   `// Function for fibonacci`   `int` `fib(``int` `n)` `{` `    ``// Stop condition` `    ``if` `(n == 0)` `        ``return` `0;`   `    ``// Stop condition` `    ``if` `(n == 1 || n == 2)` `        ``return` `1;`   `    ``// Recursion function` `    ``else` `        ``return` `(fib(n - 1) + fib(n - 2));` `}`   `// Driver Code` `int` `main()` `{` `    ``// Initialize variable n.` `    ``int` `n = 5;` `    ``cout<<``"Fibonacci series of 5 numbers is: "``;`   `    ``// for loop to print the fibonacci series.` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{` `        ``cout<

## C

 `// C code to implement Fibonacci series` `#include `   `// Function for fibonacci` `int` `fib(``int` `n)` `{` `    ``// Stop condition` `    ``if` `(n == 0)` `        ``return` `0;`   `    ``// Stop condition` `    ``if` `(n == 1 || n == 2)` `        ``return` `1;`   `    ``// Recursion function` `    ``else` `        ``return` `(fib(n - 1) + fib(n - 2));` `}`   `// Driver Code` `int` `main()` `{` `    ``// Initialize variable n.` `    ``int` `n = 5;` `    ``printf``(``"Fibonacci series "` `           ``"of %d numbers is: "``,` `           ``n);`   `    ``// for loop to print the fibonacci series.` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``printf``(``"%d "``, fib(i));` `    ``}` `    ``return` `0;` `}`

## Java

 `// Java code to implement Fibonacci series` `import` `java.util.*;`   `class` `GFG` `{`   `// Function for fibonacci` `static` `int` `fib(``int` `n)` `{` `    ``// Stop condition` `    ``if` `(n == ``0``)` `        ``return` `0``;`   `    ``// Stop condition` `    ``if` `(n == ``1` `|| n == ``2``)` `        ``return` `1``;`   `    ``// Recursion function` `    ``else` `        ``return` `(fib(n - ``1``) + fib(n - ``2``));` `}`   `// Driver Code` `public` `static` `void` `main(String []args)` `{` `  `  `    ``// Initialize variable n.` `    ``int` `n = ``5``;` `    ``System.out.print(``"Fibonacci series of 5 numbers is: "``);`   `    ``// for loop to print the fibonacci series.` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``{` `        ``System.out.print(fib(i)+``" "``);` `    ``}` `}` `}`   `// This code is contributed by rutvik_56.`

## Python3

 `# Python code to implement Fibonacci series`   `# Function for fibonacci` `def` `fib(n):`   `    ``# Stop condition` `    ``if` `(n ``=``=` `0``):` `        ``return` `0`   `    ``# Stop condition` `    ``if` `(n ``=``=` `1` `or` `n ``=``=` `2``):` `        ``return` `1`   `    ``# Recursion function` `    ``else``:` `        ``return` `(fib(n ``-` `1``) ``+` `fib(n ``-` `2``))`     `# Driver Code`   `# Initialize variable n.` `n ``=` `5``;` `print``(``"Fibonacci series of 5 numbers is :"``,end``=``" "``)`   `# for loop to print the fibonacci series.` `for` `i ``in` `range``(``0``,n): ` `    ``print``(fib(i),end``=``" "``)`

## C#

 `using` `System;`   `public` `class` `GFG` `{`   `  ``// Function for fibonacci` `  ``static` `int` `fib(``int` `n)` `  ``{`   `    ``// Stop condition` `    ``if` `(n == 0)` `      ``return` `0;`   `    ``// Stop condition` `    ``if` `(n == 1 || n == 2)` `      ``return` `1;`   `    ``// Recursion function` `    ``else` `      ``return` `(fib(n - 1) + fib(n - 2));` `  ``}`   `  ``// Driver Code` `  ``static` `public` `void` `Main ()` `  ``{`   `    ``// Initialize variable n.` `    ``int` `n = 5;` `    ``Console.Write(``"Fibonacci series of 5 numbers is: "``);`   `    ``// for loop to print the fibonacci series.` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{` `      ``Console.Write(fib(i) + ``" "``);` `    ``}` `  ``}` `}`   `// This code is contributed by avanitrachhadiya2155`

## Javascript

 ``

Output

`Fibonacci series of 5 numbers is: 0 1 1 2 3 `

Time Complexity: O(2n)
Auxiliary Space: O(n)

Here is the recursive tree for input 5 which shows a clear picture of how a big problem can be solved into smaller ones.
fib(n) is a Fibonacci function. The time complexity of the given program can depend on the function call.

fib(n) -> level CBT (UB) -> 2^n-1 nodes -> 2^n function call -> 2^n*O(1) -> T(n) = O(2^n)

For Best Case.

`T(n) =   θ(2^n\2)`

Working: Problem 2: Write a program and recurrence relation to find the Factorial of n where n>2 .
Mathematical Equation:

```1 if n == 0 or n == 1;
f(n) = n*f(n-1) if n> 1;```

Recurrence Relation:

```T(n) = 1 for n = 0
T(n) = 1 + T(n-1) for n > 0```

Recursive Program:
Input: n = 5
Output:
factorial of 5 is: 120
Implementation:

## C++

 `// C++ code to implement factorial` `#include ` `using` `namespace` `std;`   `// Factorial function` `int` `f(``int` `n)` `{` `    ``// Stop condition` `    ``if` `(n == 0 || n == 1)` `        ``return` `1;`   `    ``// Recursive condition` `    ``else` `        ``return` `n * f(n - 1);` `}`   `// Driver code` `int` `main()` `{` `    ``int` `n = 5;` `    ``cout<<``"factorial of "``<

## C

 `// C code to implement factorial` `#include `   `// Factorial function` `int` `f(``int` `n)` `{` `    ``// Stop condition` `    ``if` `(n == 0 || n == 1)` `        ``return` `1;`   `    ``// Recursive condition` `    ``else` `        ``return` `n * f(n - 1);` `}`   `// Driver code` `int` `main()` `{` `    ``int` `n = 5;` `    ``printf``(``"factorial of %d is: %d"``, n, f(n));` `    ``return` `0;` `}`

## Java

 `// Java code to implement factorial` `public` `class` `GFG` `{`   `  ``// Factorial function` `  ``static` `int` `f(``int` `n)` `  ``{`   `    ``// Stop condition` `    ``if` `(n == ``0` `|| n == ``1``)` `      ``return` `1``;`   `    ``// Recursive condition` `    ``else` `      ``return` `n * f(n - ``1``);` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `main(String[] args) ` `  ``{` `    ``int` `n = ``5``;` `    ``System.out.println(``"factorial of "` `+ n + ``" is: "` `+ f(n));` `  ``}` `}`   `// This code is contributed by divyesh072019.`

## Python3

 `# Python3 code to implement factorial`   `# Factorial function` `def` `f(n):`   `    ``# Stop condition` `    ``if` `(n ``=``=` `0` `or` `n ``=``=` `1``):` `        ``return` `1``;`   `    ``# Recursive condition` `    ``else``:` `        ``return` `n ``*` `f(n ``-` `1``);`     `# Driver code` `if` `__name__``=``=``'__main__'``:`   `    ``n ``=` `5``;` `    ``print``(``"factorial of"``,n,``"is:"``,f(n))` `    `  `    ``# This code is contributed by pratham76.`

## C#

 `// C# code to implement factorial` `using` `System;` `class` `GFG {`   `  ``// Factorial function` `  ``static` `int` `f(``int` `n)` `  ``{` `    ``// Stop condition` `    ``if` `(n == 0 || n == 1)` `      ``return` `1;`   `    ``// Recursive condition` `    ``else` `      ``return` `n * f(n - 1);` `  ``}`   `  ``// Driver code` `  ``static` `void` `Main()` `  ``{` `    ``int` `n = 5;` `    ``Console.WriteLine(``"factorial of "` `+ n + ``" is: "` `+ f(n));` `  ``}` `}`   `// This code is contributed by divyeshrabadiya07.`

## Javascript

 ``

Output

`factorial of 5 is: 120`

Time complexity: O(n)
Auxiliary Space: O(n)

Working:

What are the disadvantages of recursive programming over iterative programming?
Note that both recursive and iterative programs have the same problem-solving powers, i.e., every recursive program can be written iteratively and vice versa is also true. The recursive program has greater space requirements than the iterative program as all functions will remain in the stack until the base case is reached. It also has greater time requirements because of function calls and returns overhead.

Moreover, due to the smaller length of code, the codes are difficult to understand and hence extra care has to be practiced while writing the code. The computer may run out of memory if the recursive calls are not properly checked.

What are the advantages of recursive programming over iterative programming?
Recursion provides a clean and simple way to write code. Some problems are inherently recursive like tree traversals, Tower of Hanoi, etc. For such problems, it is preferred to write recursive code. We can write such codes also iteratively with the help of a stack data structure. For example refer Inorder Tree Traversal without Recursion, Iterative Tower of Hanoi.

Summary of Recursion:

• There are two types of cases in recursion i.e. recursive case and a base case.
• The base case is used to terminate the recursive function when the case turns out to be true.
• Each recursive call makes a new copy of that method in the stack memory.
• Infinite recursion may lead to running out of stack memory.
• Examples of Recursive algorithms: Merge Sort, Quick Sort, Tower of Hanoi, Fibonacci Series, Factorial Problem, etc.

Output based practice problems for beginners:
Practice Questions for Recursion | Set 1
Practice Questions for Recursion | Set 2
Practice Questions for Recursion | Set 3
Practice Questions for Recursion | Set 4
Practice Questions for Recursion | Set 5
Practice Questions for Recursion | Set 6
Practice Questions for Recursion | Set 7
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 write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above

My Personal Notes arrow_drop_up
Recommended Articles
Page :