Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
C++ Recursion: Syntax & Structure
3.
Example of a Recursive Function in C++
3.1.
C++
4.
Understanding Base Condition & Recursive Case in C++ Recursion
4.1.
Base Condition in Recursion
4.2.
Recursive Case in Recursion
4.3.
Example to show Base Condition & Recursive Case : 
4.4.
C++
5.
Examples of Recursion
5.1.
Calculating Fibonacci Numbers
5.1.1.
Fibonacci Recursive Function in C++
5.2.
C++
5.3.
Sum of Natural Numbers
5.3.1.
Recursive Function to Calculate Sum:
5.4.
C++
5.5.
Tree Traversal
5.5.1.
Recursive Function for Inorder Traversal:
5.6.
C++
6.
Working of Recursion in C++
6.1.
What is Call Stack ? 
6.2.
C++
6.3.
Memory Management in Recursive Calls
6.4.
Memory Management in C++ Recursion
6.5.
How Recursion Affects Memory
6.6.
Preventing Stack Overflow
7.
Example: Optimizing Recursive Functions
7.1.
C++
8.
What is Stack Overflow in C++ Recursion?
8.1.
Explaining Stack Overflow
8.2.
Causes of Stack Overflow in Recursive Programs
8.3.
Example of Stack Overflow
8.4.
C++
8.5.
Preventing Stack Overflow
9.
Types of Recursion in C++
9.1.
Direct Recursion
9.2.
C++
9.3.
Indirect Recursion
9.4.
C++
9.5.
Choosing the Right Type of Recursion
10.
Applications of Recursion in C++
10.1.
Sorting Algorithms
10.2.
C++
10.3.
2. Searching Algorithms
10.4.
C++
10.5.
3. Generating Permutations
10.6.
4. Dynamic Programming
10.7.
5. Backtracking Algorithms
10.8.
6. Tree Traversals
11.
Drawbacks of Recursion in C++
11.1.
1. Stack Overflow Risk
11.2.
2. Performance Overhead
11.3.
3. Memory Inefficiency
11.4.
4. Debugging Complexity
11.5.
5. Logical Complexity
11.6.
6. Difficulty in Conversion to Iteration
12.
Example of a Recursive Function with Potential Drawbacks:
12.1.
C++
13.
Frequently Asked Questions
13.1.
What is the main advantage of using recursion?
13.2.
When should recursion be avoided in C++ programming?
13.3.
How can the risk of stack overflow in recursion be minimized?
14.
Conclusion
Last Updated: May 6, 2024
Easy

Recursion in C++

Author Pallavi singh
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Recursion is a powerful programming technique where a function calls itself to solve a problem by breaking it down into smaller sub-problems. It's a fundamental concept in programming that allows you to write elegant & concise solutions to complex problems. 

Recursion in C++

In this article, we'll learn recursion in C++, and will talk about its syntax, types, examples, & applications in detail.

C++ Recursion: Syntax & Structure

Recursion in C++ involves a function calling itself to perform a task. This process reduces complex problems into simpler ones until a base condition is met, which stops the recursion. To implement recursion, you structure a function so it includes two main components: a base case and a recursive case.

  • Base Case: This is the condition under which the recursion stops; it prevents the function from calling itself indefinitely. Without a base case, a recursive function would lead to infinite recursion, potentially causing a program crash due to stack overflow.
     
  • Recursive Case: This is the part of the function where the recursion occurs. The function includes a call to itself with a different argument, gradually approaching the base case.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Example of a Recursive Function in C++

Consider a function that calculates the factorial of a number. A factorial of a number n (denoted as n!) is the product of all positive integers less than or equal to n.

  • C++

C++

#include <iostream>
using namespace std;

// Function to calculate factorial
int factorial(int n) {
// Base case
if (n == 0) return 1; // Factorial of 0 is 1

// Recursive case
return n * factorial(n - 1);
}

int main() {
int num = 5;
cout << "Factorial of " << num << " is " << factorial(num) << endl;
return 0;
}

Output

Factorial of 5 is 120

In this code, the factorial function calls itself with n-1 until it reaches the base case where n equals 0. When n is 0, the function returns 1, and the recursion unwinds, calculating the factorial by multiplying the returned values.

Understanding Base Condition & Recursive Case in C++ Recursion

Base Condition in Recursion

The base condition in a recursive function is critical as it stops the recursion. It is the simplest case, where the solution is known and can be returned directly without any further recursive calls. Think of it as the exit point for the recursive process. If this condition is not met, the recursion would continue indefinitely, leading to a stack overflow, which is a common error in recursive programming.

Recursive Case in Recursion

The recursive case is where the function continues to call itself. Each call to the function reduces the original problem into a smaller sub-problem. This step is crucial because it moves the problem closer to the base condition with each iteration. The recursive case must always work towards reaching the base condition; otherwise, it can result in infinite recursion.

Example to show Base Condition & Recursive Case : 

We will use a recursive function to calculate the sum of all numbers up to a given number n. This is a straightforward problem where we can clearly define the base and recursive cases.

  • C++

C++

#include <iostream>
using namespace std;

// Function to calculate the sum of numbers from 1 to n
int sum(int n) {
// Base condition
if (n == 1) return 1; // The sum of 1 is trivially 1

// Recursive case
return n + sum(n - 1); // Sum of numbers up to n is n plus the sum of numbers up to n-1
}

int main() {
int num = 10;
cout << "Sum of numbers from 1 to " << num << " is " << sum(num) << endl;
return 0;
}

Output

Sum of numbers from 1 to 10 is 55


In this example:

  • The base condition is when n is 1. In this case, the sum of numbers from 1 to 1 is just 1, so the function returns 1 directly.
     
  • The recursive case takes the current number n and adds it to the result of the sum of all numbers up to n-1. This breakdown continues until the base condition is reached, at which point the recursion unwinds, summing up all the individual results to give the final sum.
     

Note -: By setting a clear base condition and a properly defined recursive case, we ensure that the recursion is effective and terminates appropriately.

Examples of Recursion

Calculating Fibonacci Numbers

Recursion can be understood easily with the Fibonacci sequence, a series where each number is the sum of the two preceding ones, usually starting with 0 and 1. This example is popular in learning recursion due to its simplicity and the clear illustration of how recursion breaks down a problem into manageable parts.

Fibonacci Recursive Function in C++

Here's how you can write a C++ program to find the nth Fibonacci number using recursion:

  • C++

C++

#include <iostream>
using namespace std;

// Function to calculate nth Fibonacci number
int fibonacci(int n) {
// Base cases
if (n == 0) return 0; // The first number of the Fibonacci sequence is 0
if (n == 1) return 1; // The second number of the Fibonacci sequence is 1

// Recursive case
return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
int position = 10;
cout << "The 10th Fibonacci number is: " << fibonacci(position) << endl;
return 0;
}

Output

The 10th Fibonacci number is: 55


In this program:

  1. The base cases address the first two numbers of the Fibonacci sequence. When the function fibonacci(n) is called with n as 0 or 1, it returns 0 and 1, respectively.
     
  2. The recursive case calls fibonacci(n - 1) + fibonacci(n - 2), calculating the nth Fibonacci number by summing the two preceding Fibonacci numbers. This continues recursively until it reaches the base cases.

Sum of Natural Numbers

A simple example of recursion is calculating the sum of natural numbers up to a given number. This problem can be broken down into adding a number to the sum of all numbers before it, which is a perfect scenario for recursion.

Recursive Function to Calculate Sum:

  • C++

C++

#include <iostream>
using namespace std;

// Function to calculate the sum of natural numbers up to n
int sumOfNaturalNumbers(int n) {
if (n == 0) return 0; // Base case: the sum of numbers up to 0 is 0
return n + sumOfNaturalNumbers(n - 1); // Recursive case
}

int main() {
int num = 10;
cout << "Sum of natural numbers up to " << num << " is " << sumOfNaturalNumbers(num) << endl;
return 0;
}

Output

Sum of natural numbers up to 10 is 55


In this example, the function sumOfNaturalNumbers recursively calls itself, each time with a decremented value of n, until it reaches 0, which is the base case where it stops calling itself.

Tree Traversal

Recursion is extensively used in data structures like trees for traversal operations. Tree traversal algorithms like Inorder, Preorder, and Postorder are typically implemented recursively.

Recursive Function for Inorder Traversal:

  • C++

C++


#include <iostream>
using namespace std;

struct Node {
int data;
Node* left;
Node* right;

Node(int value) : data(value), left(nullptr), right(nullptr) {}
};

// Function to perform inorder traversal of a binary tree
void inorderTraversal(Node* root) {
if (root != nullptr) {
inorderTraversal(root->left); // Traverse left subtree
cout << root->data << " "; // Visit the root
inorderTraversal(root->right); // Traverse right subtree
}
}

int main() {
// Creating a simple binary tree: 1
// / \
// 2 3
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);

cout << "Inorder traversal of the tree is: ";
inorderTraversal(root);
cout << endl;

return 0;
}

Output

Inorder traversal of the tree is: 2 1 3 


In the binary tree traversal example, the function inorderTraversal calls itself to visit each node in the left subtree, then processes the current node, and finally calls itself to visit each node in the right subtree. This recursive pattern efficiently handles the tree structure.

Working of Recursion in C++

When a recursive function is called in C++, each call creates a new layer in the call stack, which is an area of memory that stores active subroutines of a program.

What is Call Stack ? 

Every time a recursive function is called, a new frame is pushed onto the call stack. This frame contains the local variables and the return address of the function. Each recursive call adds another frame to the stack until the base condition is reached. When the base condition is met, the function starts returning values, and each frame is popped from the stack one by one, unwinding the recursion.

Here is a simple example using the factorial function to show how recursion works in C++:

  • C++

C++

#include <iostream>
using namespace std;

// Function to calculate factorial
int factorial(int n) {
cout << "Calculating factorial(" << n << ")" << endl;
if (n == 0) return 1; // Base case
int result = n * factorial(n - 1);
cout << "Returning " << result << " for factorial(" << n << ")" << endl;
return result;
}

int main() {
int num = 5;
cout << "Factorial of " << num << " is " << factorial(num) << endl;
return 0;
}

Output

Factorial of 5 is Calculating factorial(5)
Calculating factorial(4)
Calculating factorial(3)
Calculating factorial(2)
Calculating factorial(1)
Calculating factorial(0)
Returning 1 for factorial(1)
Returning 2 for factorial(2)
Returning 6 for factorial(3)
Returning 24 for factorial(4)
Returning 120 for factorial(5)
120


In this example:

  • The function factorial is called with n equal to 5.
     
  • The recursion continues deeper, calling factorial with 4, 3, 2, 1, and finally 0.
     
  • When n is 0, the function hits the base case and returns 1.
     
  • The call stack then begins to unwind, with each function call completing and returning its result to the previous caller.
     
  • This process continues until the original call to factorial(5) completes and returns the final result.

Memory Management in Recursive Calls

It's essential to understand that each recursive call consumes stack memory. Excessive recursion can lead to a stack overflow if the recursion depth is too large or if the base case is not properly defined to stop the recursion. Managing memory and understanding the limits of recursion are key to using it effectively without crashing your program.

Memory Management in C++ Recursion

Proper memory management is critical when using recursion in C++. Each recursive call requires a portion of the stack memory, which is limited. Mismanagement can lead to stack overflow, especially with deep recursion or large data sets.

How Recursion Affects Memory

When a recursive function is called, C++ allocates memory on the call stack for the function’s parameters, local variables, and the return address. This memory remains allocated until the function completes and returns. In recursive functions, since each call creates a new entry on the stack, excessive recursive calls can fill up the stack space, leading to a stack overflow error.

Preventing Stack Overflow

To manage memory efficiently and prevent stack overflow in recursive functions, consider the following strategies:

  1. Limiting Recursion Depth: Set a reasonable limit to the depth of recursion to prevent excessive stack use. This can be done by adding conditions in your function to stop recursion beyond a certain depth.
     
  2. Tail Recursion: Whenever possible, use tail recursion. In tail recursion, the recursive call is the last operation in the function. This allows the compiler to optimize the recursion, reusing the stack frame for each call, thus using less stack space.
     
  3. Iterative Solutions: When practical, convert recursive algorithms to iterative ones. Iterative solutions use explicit looping constructs and typically require less stack space than recursive solutions.

Example: Optimizing Recursive Functions

Consider a recursive function that calculates the nth Fibonacci number. The naive recursive approach can be optimized using tail recursion or converted into an iterative solution to manage memory better:

  • C++

C++

#include <iostream>
using namespace std;

// Tail Recursive Function to calculate nth Fibonacci number
int fibonacci(int n, int a = 0, int b = 1) {
if (n == 0) return a;
if (n == 1) return b;
return fibonacci(n - 1, b, a + b);
}

// Iterative Function to calculate nth Fibonacci number
int fibonacciIterative(int n) {
int a = 0, b = 1, c;
if (n == 0) return a;
for (int i = 2; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
return b;
}

int main() {
int position = 10;
cout << "The 10th Fibonacci number (recursive) is: " << fibonacci(position) << endl;
cout << "The 10th Fibonacci number (iterative) is: " << fibonacciIterative(position) << endl;
return 0;
}

Output

he 10th Fibonacci number (recursive) is: 55
The 10th Fibonacci number (iterative) is: 55

 

In this example:

  • The tail-recursive fibonacci function is optimized by the compiler to reuse the stack frame.
     
  • The iterative fibonacciIterative function does not use recursion, thus preventing any risk of stack overflow regardless of the value of n.

What is Stack Overflow in C++ Recursion?

Stack overflow is a common issue in programming that occurs when there is more information on the call stack than it can handle. This usually happens in recursive functions if the recursion goes too deep without an adequate base case to terminate it.

Explaining Stack Overflow

The stack is a special region of your computer's memory that stores temporary variables created by each function (including the main function). The size of the stack is not infinite. When a program uses more stack space than is available, it results in a stack overflow error. In the context of recursion, each function call adds a new layer to the stack, which includes the function’s parameters and local variables. If the recursive calls do not end (or end too late), the stack runs out of space and the program crashes.

Causes of Stack Overflow in Recursive Programs

  • Deep Recursion: When a recursive function calls itself too many times before reaching a base case, it can use up all the stack space. This is often due to a base case that is not correctly defined or a problem that naturally requires deep recursion.
     
  • Large Stack Frames: If each function call uses a lot of memory (for example, due to large data structures as parameters), the stack may fill up quickly, even with relatively shallow recursion depths.

Example of Stack Overflow

Here is a simple example where a stack overflow might occur if the base case is not adequately handled:

  • C++

C++

#include <iostream>
using namespace std;

// Recursive function without proper base case
void recursiveFunction(int num) {
cout << num << endl;
// Incorrect or missing base case
recursiveFunction(num + 1);
}

int main() {
recursiveFunction(1);
return 0;
}

Output

Ouput

In this example, recursiveFunction continues to call itself with no termination condition. Eventually, the continual allocation of new frames for each function call will exceed the stack capacity, leading to a stack overflow error.

Preventing Stack Overflow

  1. Ensure that every recursive function has a proper base case.
     
  2. Consider using iterative methods if the depth of recursion could be very high.
     
  3. Use algorithms that reduce the depth of recursion, such as tail recursion optimization.

Types of Recursion in C++

Recursion in C++ can be categorized into several types based on how functions call themselves. Understanding these types can help you choose the right approach for different problems and optimize your recursive solutions effectively.

Direct Recursion

Direct recursion occurs when a function calls itself directly. This is the most straightforward and common form of recursion. It's used in many basic recursive algorithms, such as calculating factorials or Fibonacci numbers.

Example : 

  • C++

C++

#include <iostream>
using namespace std;

// Function to calculate factorial using direct recursion
int factorial(int n) {
if (n == 0) return 1; // Base case
return n * factorial(n - 1); // Recursive call
}

int main() {
int num = 5;
cout << "Factorial of " << num << " is " << factorial(num) << endl;
return 0;
}

Output

Factorial of 5 is 120


In the above example, the factorial function calls itself directly to compute the product of numbers down to 1.

Indirect Recursion

Indirect recursion happens when a function calls another function, and that function calls the first function, creating a cycle. This type of recursion can involve two or more functions that call each other in a cycle. It’s less common but can be useful for solving certain types of problems where functions naturally alternate between states or operations.

Example : 

  • C++

C++

#include <iostream>
using namespace std;

// Forward declaration of functions
void functionA(int n);
void functionB(int n);

// Function A calls Function B
void functionA(int n) {
if (n > 0) {
cout << "A: " << n << endl;
functionB(n - 1);
}
}

// Function B calls Function A
void functionB(int n) {
if (n > 1) {
cout << "B: " << n << endl;
functionA(n / 2);
}
}

int main() {
functionA(20);
return 0;
}

Output

A: 20
B: 19
A: 9
B: 8
A: 4
B: 3
A: 1


 In the above example, functionA and functionB call each other, creating a loop of calls that reduces the number incrementally and then by division.

Choosing the Right Type of Recursion

  • Direct recursion is often simpler and clearer, especially for well-understood mathematical problems.
     
  • Indirect recursion can be more suitable for problems where different phases or steps are best handled by different functions.

Applications of Recursion in C++

Sorting Algorithms

Recursion forms the core of several efficient sorting algorithms, such as Quicksort and Mergesort. These algorithms divide the array into smaller parts, sort them recursively, and then combine them to produce a sorted array.

Example: Quicksort Algorithm

  • C++

C++

#include <iostream>
using namespace std;

void quicksort(int arr[], int low, int high) {
if (low < high) {
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element

for (int j = low; j <= high - 1; j++) {
// If current element is smaller than the pivot
if (arr[j] < pivot) {
i++; // increment index of smaller element
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
int pi = i + 1;

// Recursively sort elements before partition and after partition
quicksort(arr, low, pi - 1);
quicksort(arr, pi + 1, high);
}
}

int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quicksort(arr, 0, n - 1);
cout << "Sorted array: ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
return 0;
}

Output

Sorted array: 1 5 7 8 9 10 

2. Searching Algorithms

Recursive techniques are used in algorithms like Binary Search, where the problem space is halved each time, making the search process faster than linear searching.

Example: Binary Search

  • C++

C++

#include <iostream>
using namespace std;

int binarySearch(int arr[], int l, int r, int x) {
if (r >= l) {
int mid = l + (r - l) / 2;

// If the element is present at the middle itself
if (arr[mid] == x) return mid;

// If element is smaller than mid, then it can only be present in left subarray
if (arr[mid] > x) return binarySearch(arr, l, mid - 1, x);

// Else the element can only be present in right subarray
return binarySearch(arr, mid + 1, r, x);
}

// Element is not present in array
return -1;
}

int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
cout << "Element is found at index " << result << endl;
return 0;
}

Output

Element is found at index 3

3. Generating Permutations

Recursion is used to generate all possible permutations of a set of data, such as characters in a string or elements in an array. This is particularly useful in scenarios where exploring all possible configurations of data elements is necessary, such as in password cracking, game solving, or generating possible scenarios in simulation studies.

4. Dynamic Programming

Many dynamic programming problems, like calculating the nth Fibonacci number, involve recursive solutions with memoization, which stores the results of expensive function calls and reuses them when the same inputs occur again, thus saving computation time.

5. Backtracking Algorithms

Recursion is a fundamental aspect of backtracking algorithms, which are used in solving constraint satisfaction problems like puzzles (e.g., Sudoku), and in combinatorial optimization problems (e.g., finding the minimum path in a labyrinth).

6. Tree Traversals

In computer science, trees are a fundamental data structure, and recursion is the natural approach for traversing them. Operations such as printing all nodes of a tree, searching for a value, or computing the height of the tree are elegantly implemented using recursion.

Drawbacks of Recursion in C++

1. Stack Overflow Risk

Recursion relies heavily on the call stack to manage returns and local variables for each call. If the recursion is too deep, it can consume all the available stack memory, leading to a stack overflow. This is particularly problematic with poorly defined base cases or with problems that inherently require a deep level of recursion.

2. Performance Overhead

Each recursive call involves overhead, including saving the state of the function and managing the call stack. This overhead can make recursive solutions slower and less efficient compared to their iterative counterparts, especially for functions that are called with a high frequency.

3. Memory Inefficiency

Recursion can lead to significant memory use due to the need to store the context of multiple function calls. Each recursive call stores information on the stack, and if the recursion depth is substantial, it can lead to high memory consumption.

4. Debugging Complexity

Debugging recursive functions can be more challenging than debugging iterative solutions. The recursive calls can make it harder to follow the flow of execution, especially when the recursion goes several levels deep.

5. Logical Complexity

Writing a recursive function requires a clear understanding of the problem and how it breaks down into smaller, manageable sub-problems. This can sometimes lead to complex logic that is difficult to follow, making the code less readable and maintainable.

6. Difficulty in Conversion to Iteration

Some recursive algorithms, especially those that are not tail-recursive, can be challenging to convert into iterative forms. This conversion might be necessary to optimize performance or to avoid stack overflow, but achieving it requires a deep understanding of both recursion and iteration, which can be a significant barrier.

Example of a Recursive Function with Potential Drawbacks:

Consider a simple recursive function to calculate the sum of an array:

  • C++

C++

#include <iostream>
using namespace std;

// Function to calculate the sum of an array using recursion
int sumArray(int arr[], int n) {
if (n <= 0) return 0; // Base case
return arr[n-1] + sumArray(arr, n-1); // Recursive case
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Sum of array is " << sumArray(arr, n) << endl;
return 0;
}

Output

Sum of array is 15


In this example:

  • Stack Overflow Risk: If n is very large, the recursive calls could exhaust the stack.
     
  • Performance Overhead & Memory Inefficiency: Each recursive call adds overhead and consumes stack space.
     
  • Debugging Complexity: Tracing through each recursive call can be cumbersome if there are issues or errors.

Frequently Asked Questions

What is the main advantage of using recursion?

The main advantage of using recursion is its ability to simplify the code for complex problems by breaking them down into simpler, more manageable sub-problems.

When should recursion be avoided in C++ programming?

Recursion should be avoided when the problem involves a large number of function calls that could lead to stack overflow, or when performance and memory efficiency are critical.

How can the risk of stack overflow in recursion be minimized?

To minimize the risk of stack overflow in recursion, ensure a well-defined base case is present, limit recursion depth, use tail recursion where possible, and consider converting recursive processes to iterative ones if feasible.

Conclusion

In this article, we learned the recursion in C++, from its basic syntax and structure to various practical examples and applications. We've discussed the types of recursion, such as direct and indirect recursion, with proper examples to clear the difference. We discussed the disadvantage of using recursion, such as the risk of stack overflow, performance overhead, and debugging complexity.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Previous article
Factorial Program in C++
Next article
Advantages of C++
Live masterclass