C++ Recursion (original) (raw)

Last Updated : 27 May, 2026

Recursion is a programming technique where a function calls itself repeatedly to solve a problem.The recursive calls continue until a specific base condition is met, which stops the recursion.

#include using namespace std;

void printHello(int n) {

// Base Case
if (n == 0) return;

cout << "Hello" << endl;

printHello(n - 1);

}

int main() { printHello(5); return 0; }

`

Output

Hello Hello Hello Hello Hello

**Explanation:

Recursive Function

A function that calls itself is called a recursive function. When a recursive function is called, it executes a set of instructions and then calls itself to execute the same set of instructions with a smaller input. A recursive function should contain,

returntype function(parameters) {

// base case
if (base condition) {
    return base value;
}

// recursive case
return recursive expression involving function(modified parameters);

}

`

This structure allows problems to be broken down into simpler versions of themselves, making recursion a powerful tool for solving problems that can be defined in terms of smaller instances.

According to this, from the first example, we can deduce that:

**Base Condition

if (n == 0) return;

**Recursive Case

printHello(n - 1);

Working

To understand how recursion works internally, it is important to understand the behavior of the call stack during recursive function calls.
Whenever a function calls itself, the current function state is stored in the stack, and new recursive calls continue until the base case is reached.

The following example demonstrates both the descending phase (going deeper into recursion) and the ascending phase (returning back from recursion):

C++ `

#include using namespace std;

void f(int n) {

cout << "F(" << n << ")'s Stack Frame Pushed\n";
if (n > 1) {
    f(n - 1);
    f(n - 1);
}
cout << "F(" << n << ")'s Stack Frame Removed\n";

}

int main() { f(3); return 0; }

`

Output

F(3)'s Stack Frame Pushed F(2)'s Stack Frame Pushed F(1)'s Stack Frame Pushed F(1)'s Stack Frame Removed F(1)'s Stack Frame Pushed F(1)'s Stack Frame Removed F(2)'s Stack Frame Removed F(2)'s Stack Fr...

In this program, the function f(int n) prints messages when a recursive call is added to the stack (Stack Frame Pushed) and when it is removed (Stack Frame Removed). This helps visualize how the call stack behaves during recursion.

**Descending Phase: The function keeps calling itself with n - 1 while n > 1, causing new stack frames to be pushed onto the call stack and increasing its size.

**Ascending Phase: Once the base case is reached, the function starts returning back. Each completed call removes its stack frame, and control moves to the previous function call, showing the stack unwinding process clearly.

For f(3), the recursive calls form a tree-like pattern, and the output clearly shows when each function call starts and ends during recursion.

**Illustrations:

This example follows a tree recursion pattern, where each function call makes multiple recursive calls (in this case, two). It’s one of the common forms of recursion.

To explore more patterns like _linear recursion, _tail recursion, and _indirect recursion, check out our detailed article on Types of Recursion.

Memory Management in Recursion

Like other functions, the data of a recursive function is stored in stack memory as a stack frame. This stack frame is removed once the function returns a value. In recursion,

Refer these article to know more about Function Call Stack, Difference between recursion and iteration

Stack Overflow

Stack Overflow is an error that occurs when a program uses more stack memory than the system allows, usually because of too many recursive function calls.
It commonly happens when recursion does not stop due to a missing or incorrect base condition.

Applications of Recursion

Recursion is widely used in computer science and programming. Common applications include:

Overall, recursion is a powerful and versatile technique for solving a wide range of problems.

Limitations of Recursion

Recursion is a powerful programming technique, but it also has some limitations that can affect performance and memory usage in certain situations.