Recursion is a programming technique in which a function calls itself to solve smaller instances of the same problem. It continues making recursive calls until a base case is reached, which stops the recursion.
- A recursive function contains a call to itself either directly or indirectly.
- Every recursive call creates a new stack frame, and execution returns in reverse order after reaching the base case.
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.
#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
Explanation
- The show() function calls itself with a smaller value of n, making it a recursive function.
- This process continues until the base case is reached, after which the recursive calls return one by one, completing the execution.
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.
#include <stdio.h>
void head(int n) {
if (n == 0)
return;
// Recursive call before any processing
head(n - 1);
printf("%d ", n);
}
int main() {
head(5);
return 0;
}
Output
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.
#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:
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.
#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.
#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.

Working
A recursive function solves a problem by calling itself with a smaller or modified input. The recursive calls continue until a base case is reached, after which the function returns and completes the remaining calls in reverse order.
- The function checks the base case to determine whether recursion should stop.
- If the base case is not met, the function calls itself with a smaller or updated input.
- Each recursive call creates a new stack frame in the call stack.
- Once the base case is reached, the stack frames are removed one by one until execution completes.
Advantages
Recursion provides a simple and effective way to solve problems that can be divided into smaller subproblems.
- Produces shorter and more readable code for recursive problems.
- Simplifies the implementation of tree traversal, graph traversal, and divide-and-conquer algorithms.
- Eliminates the need for complex iterative logic in many cases.
- Naturally suits problems such as factorial, Fibonacci series, and binary search.
Limitations
Although recursion makes many algorithms easier to implement, it can increase memory usage and execution time.
- Each recursive call creates a new stack frame, increasing memory consumption.
- Deep recursion may cause a stack overflow if the recursion depth becomes too large.
- Function call overhead can make recursive solutions slower than iterative ones.
- Missing or incorrect base cases can result in infinite recursion and program failure.