Types of Recursion in C
Recursion is the process in which a function calls itself directly or indirectly to perform the same task it is doing but for some other data. It is possible by adding a call to the same function inside its body.
According to the number and position of this call, recursion can be classified into the following types:

Let's take a look at each of them one by one.
1. Direct Recursion
This is the simplest and most common form of recursion. In direct recursion, a function calls itself directly within its own body. For Example:
#include <stdio.h>
void show(int n) {
if (n == 0)
return;
printf("%d ", n);
// Direct recursive call
show(n - 1);
}
int main() {
show(5);
return 0;
}
Output
5 4 3 2 1
In this example, the show() function is directly calling itself with a smaller value of n. We can clearly see that the function call is present in its body.
Direct recursion can be further classified into two more types:
A. Head Recursion
In head recursion, the recursive call is made before any other statement in the function. So, the function first calls itself and then processes the result. For Example,
#include <stdio.h>
void head(int n) {
if (n != 0) {
// Recursive call before any processing
head(n - 1);
}
printf("%d ", n);
}
int main() {
head(5);
return 0;
}
Output
0 1 2 3 4 5
Here, the printing happens after the recursive call, which means the function first goes deep into recursion and then starts printing while returning.

B. Tail Recursion
Tail recursion is the opposite of head recursion. In this, the function performs its task first and then calls itself. The recursive call is the last operation in the function. For Example,
#include <stdio.h>
void tail(int n) {
if (n == 0)
return;
printf("%d ", n);
// Recursive call after processing
tail(n - 1);
}
int main() {
tail(5);
return 0;
}
Output
5 4 3 2 1

Tail recursion is more memory-efficient than head recursion and can sometimes be optimized by the compiler. To know more, refer to the article - Tail Call Optimisation in C - GeeksforGeeks
C. Tree Recursion
In tree recursion, a function makes more than one recursive call to itself within its body. As a result, the recursion tree branches out. For Example,
#include <stdio.h>
void tree(int n) {
if (n == 0)
return;
printf("%d ", n);
// First recursive call
tree(n - 1);
// Second recursive call
tree(n - 1);
}
int main() {
tree(3);
return 0;
}
Output
3 2 1 1 2 1 1

As you can see, the function is called twice for each value, which leads to a tree-like recursive structure.
D. Nested Recursion
Nested recursion occurs when a recursive functionâs argument itself is a recursive function call. For Example,
#include <stdio.h>
int nested(int n) {
if (n > 100)
return n - 10;
// Two recursive calls nested inside
// each other.
return nested(nested(n + 11));
}
int main() {
printf("%d", nested(95));
return 0;
}
Output
91
This type of recursion is more complex and is rarely used unless required for specific logic.
2. Indirect Recursion
In indirect recursion, a function doesnât call itself directly. Instead, it calls another function, which in turn calls the first one. This chain can involve more than two functions. For example,
#include <stdio.h>
void funcA(int);
void funcB(int);
void funcA(int n) {
if (n > 0) {
printf("%d ", n);
funcB(n - 1);
}
}
void funcB(int n) {
if (n > 0) {
printf("%d ", n);
funcA(n / 2);
}
}
int main() {
funcA(10);
return 0;
}
Output
10 9 4 3 1
Here, funcA() calls funcB(), and funcB() calls funcA(). They are indirectly recursive.
