Types of Recursion in C++
Last Updated :
16 Jun, 2025
Recursion is a process where a function calls itself, either directly or indirectly to repeat the same task for smaller data. In C++, recursion occurs by writing a function that includes a call to itself within its body.
Based on how and where this function calls are present, recursion in C++ can be classified into the following types:
Let’s look at each type one by one.
1. Direct Recursion
Direct recursion occurs when a function calls itself directly from within its body. This is the most common and straightforward form of recursion.
Example:
C++
#include <iostream>
using namespace std;
void show(int n) {
if (n == 0)
return;
cout << n << " ";
// Direct recursive call
show(n - 1);
}
int main() {
show(5);
return 0;
}
In this example, the show() function calls itself directly with a reduced value of n.
Direct recursion can be divided into:
A. Head Recursion
In head recursion, the recursive call happens before any processing in the function. The function calls itself first and processes later.
C++
#include <iostream>
using namespace std;
void head(int n) {
if (n != 0) {
// Recursive call before processing
head(n - 1);
}
cout << n << " ";
}
int main() {
head(5);
return 0;
}
Here, the function goes deep into recursion first and processes while returning.
B. Tail Recursion
In tail recursion, the function processes first and the recursive call is the last operation.
C++
#include <iostream>
using namespace std;
void tail(int n) {
if (n == 0)
return;
cout << n << " ";
// Recursive call after processing
tail(n - 1);
}
int main() {
tail(5);
return 0;
}
Tail recursion can be more memory-efficient and may benefit from compiler optimizations.
C. Tree Recursion
Tree recursion happens when a function calls itself more than once within its body, forming a tree-like structure.
C++
#include <iostream>
using namespace std;
void tree(int n) {
if (n == 0)
return;
cout << n << " ";
// Two recursive calls
tree(n - 1);
tree(n - 1);
}
int main() {
tree(3);
return 0;
}
The function calls itself twice for each value, branching like a tree.
D. Nested Recursion
Nested recursion means the argument to a function is itself a recursive call.
C++
#include <iostream>
using namespace std;
int nested(int n) {
if (n > 100)
return n - 10;
// Recursive call inside another recursive call
return nested(nested(n + 11));
}
int main() {
cout << nested(95);
return 0;
}
This type of recursion is complex and used only when necessary.
2. Indirect Recursion
In indirect recursion, a function does not call itself directly. Instead, it calls another function that eventually calls the first one, creating a chain of calls.
C++
#include <iostream>
using namespace std;
void funcA(int);
void funcB(int);
void funcA(int n) {
if (n > 0) {
cout << n << " ";
funcB(n - 1);
}
}
void funcB(int n) {
if (n > 0) {
cout << n << " ";
funcA(n / 2);
}
}
int main() {
funcA(10);
return 0;
}
Here, funcA() calls funcB(), which calls funcA() again, forming indirect recursion.
Similar Reads
C++ Recursion In C++, recursion is a technique in which a function calls itself repeatedly until a given condition is satisfied. It is used for solving a problem by breaking it down into smaller, simpler sub-problems. Then finding the solution of it and combining this solution to find the global solution.Basic Ex
8 min read
Trailing Return Type in C++ 11 The trailing return type syntax, a new method of indicating a function's return type, was introduced in C++11. Before C++11, the return type of a function was typically specified before the function name. However, in some cases, it could be challenging to express complex return types, especially whe
2 min read
return Statement in C++ In C++, the return statement returns the flow of the execution to the function from where it is called. This statement does not mandatorily need any conditional statements. As soon as the statement is executed, the flow of the program stops immediately and returns the control from where it was calle
4 min read
Derived Data Types in C++ The data types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. They are generally the data types that are created from the primitive data types and provide some additional functionality.In C++, there are four different derived data types:Table of Cont
4 min read
Finite and Infinite Recursion with examples The process in which a function calls itself directly or indirectly is called Recursion and the corresponding function is called a Recursive function. Using Recursion, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Tr
6 min read