Recursion is a programming technique where a function calls itself, either directly or indirectly, to solve a problem. In C, recursion provides an elegant way to solve complex problems by breaking them down into smaller, more manageable sub-problems of the same type.
A function that calls itself is known as a recursive function.
The logic of recursion is based on the mathematical principle of mathematical induction. To prevent a function from calling itself infinitely, every recursive function must follow two fundamental rules:
The base case is a specific condition where the function stops calling itself and returns a simple value. Without a base case, the function will enter an infinite recursion, eventually leading to a Stack Overflow error.
This is the part of the function where the "self-call" happens. Crucially, the arguments passed to the recursive call must be "smaller" or "closer" to the base case than the current arguments.
To understand recursion, one must understand the System Stack.
Each time a function is called, a new stack frame (activation record) is pushed onto the stack. This frame stores the function’s local variables, parameters, and the return address.
In recursion, these frames pile up as the function calls itself deeper and deeper.
Once the Base Case is reached, the function returns a value to the previous call.
The stack then begins to unwind, popping frames off one by one as each call completes its calculation until the final result is returned to main().
The factorial of a number $n$ (written as $n!$) is defined as $n \times (n-1) \times (n-2) \dots \times 1$.
Recursive Logic: $n! = n \times (n-1)!$
Base Case: $0! = 1$
#include <stdio.h> // Recursive function definition long int factorial(int n) { // 1. Base Case if (n == 0 || n == 1) { return 1; } // 2. Recursive Step else { return n * factorial(n - 1); } } int main() { int num = 5; printf("Factorial of %d is %ld\n", num, factorial(num)); return 0; }
factorial(3) calls factorial(2) → (Wait for result: $3 \times ?$)
factorial(2) calls factorial(1) → (Wait for result: $2 \times ?$)
factorial(1) hits Base Case, returns 1.
factorial(2) receives 1, calculates $2 \times 1 = 2$, returns 2.
factorial(3) receives 2, calculates $3 \times 2 = 6$, returns 6.
| Type | Description |
| Direct Recursion | Function A calls Function A. |
| Indirect Recursion | Function A calls Function B, and Function B calls Function A. |
| Tail Recursion | The recursive call is the very last action in the function. (Easier for compilers to optimize). |
| Tree Recursion | The function makes more than one recursive call (e.g., Fibonacci sequence). |
| Feature | Recursion | Iteration (Loops) |
| Logic | Solves via sub-problems. | Solves via repetition. |
| Code Size | Usually shorter and cleaner. | Longer and more complex logic. |
| Memory | Uses more memory (Stack overhead). | Uses less memory (Fixed variables). |
| Speed | Can be slower due to function calls. | Generally faster. |
| Infinite | Leads to Stack Overflow. | Leads to infinite CPU usage. |
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION