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