A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/cpp-recursion/ below:

C++ Recursion - GeeksforGeeks

C++ Recursion

Last Updated : 23 Jul, 2025

Recursion is a programming technique where a function calls itself repeatedly until a specific base condition is met. A function that performs such self-calling behavior is known as a recursive function, and each instance of the function calling itself is called a recursive call.

C++
#include <iostream>
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

This code demonstrates a simple recursive function that prints the word "Hello" five times. The function printHello(n) calls itself with a decremented value of n until it reaches the base case n == 0, at which point the recursion stops. Each recursive call prints "Hello" before making the next call, resulting in the message being printed once per call from n = 5 down to n = 1.

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,

C++
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);

How Recursion Works?

To understand how recursion works internally, it’s important to see how the call stack behaves during recursive calls. Each time a function calls itself, the current state is saved on the stack, and the new call begins. Once the base case is reached, the function starts returning back, one call at a time.

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

C++
#include <iostream>
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 a message when a new recursive call is made (Stack Frame Pushed) and when it finishes (Stack Frame Removed). This provides a clear view of how stack frames are added and removed during recursion.

For f(3), the function follows a pattern similar to a binary tree with recursive calls branching left and right. The output shows exactly when each function call begins and ends, helping visualize the stack behavior 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 StackDifference between recursion and iteration

What is Stack Overflow?

Stack overflow is one of the most common errors associated with the recursion which occurs when a function calls itself too many times. As we know that each recursive call requires separate space in the limited stack memory. When there is a large number of recursive calls or recursion goes on infinite times, this stack memory may get exhausted and may not be able to store more data leading to programs' termination.

Applications of Recursion

Recursion has many applications in computer science and programming. Here are some of the most common applications of recursion:

Overall, recursion is a powerful and versatile technique that can be used to solve a wide range of problems in programming and computer science.

Drawbacks of Recursion

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4