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:
C
#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;
}
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,
C
#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;
}
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,
C
#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;
}
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,
C
#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;
}
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,
C
#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;
}
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,
C
#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;
}
Here, funcA() calls funcB(), and funcB() calls funcA(). They are indirectly recursive.
Similar Reads
C Recursion Recursion is the process of a function calling itself repeatedly till the given condition is satisfied. A function that calls itself directly or indirectly is called a recursive function and such kind of function calls are called recursive calls.Example:C#include <bits/stdc++.h> using namespac
7 min read
Return Statement in C Pre-requisite: Functions in C C return statement ends the execution of a function and returns the control to the function from where it was called. The return statement may or may not return a value depending upon the return type of the function. For example, int returns an integer value, void retur
4 min read
Derived Data Types in C Data types in the C language can be categorized into three types, namely primitive, user-defined, and derived data types. In this article, we shall learn about derived data types. In C, the data types derived from the primitive or built-in data types are called Derived Data Types. In other words, th
4 min read
Types of User Defined Functions in C A user-defined function is one that is defined by the user when writing any program, as we do not have library functions that have predefined definitions. To meet the specific requirements of the user, the user has to develop his or her own functions. Such functions must be defined properly by the u
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
Sequence Points in C | Set 1 In this post, we will try to cover many ambiguous questions like following. Guess the output of following programs. C // PROGRAM 1 #include <stdio.h> int f1() { printf ("Geeks"); return 1;} int f2() { printf ("forGeeks"); return 1;} int main() { int p = f1() + f2(); return
4 min read
Structure of the C Program The basic structure of a C program is divided into 6 parts which makes it easy to read, modify, document, and understand in a particular format. C program must follow the below-mentioned outline in order to successfully compile and execute. Debugging is easier in a well-structured C program.Sections
5 min read
C Programming Interview Questions (2025) At Bell Labs, Dennis Ritchie developed the C programming language between 1971 and 1973. C is a mid-level structured-oriented programming and general-purpose programming. It is one of the oldest and most popular programming languages. There are many applications in which C programming language is us
15+ min read
A C/C++ Pointer Puzzle Prerequisite: Pointers Assuming the size of int = 4 bytes, size of a pointer variable = 8 byte, what will be the output of following program. Few hints on how to solve it: Size of int = 4 bytes, size of a pointer variable = 8 bytes (on my machine), adding 1 to a pointer makes the pointer point to it
2 min read
Postorder Tree Traversal in Binary Tree in C A binary tree is a hierarchical data structure in computer science. Each node in a binary tree can have at most two children: a left child and a right child. There are several ways to traverse a binary tree and in this article, we will learn about the postorder traversal of a binary tree in C. Examp
3 min read