Some problems are best solved by breaking them into smaller copies of the same problem. A function that calls itself does exactly this.
Definition and Recursive Notation
Recursion is a technique in which a function calls itself, directly or through another function. Every correct recursive definition has two parts: a base case, which is solved directly without recursion, and a recursive case, which expresses the problem in terms of a smaller instance of itself, so that the calls move towards the base case. For example, n! = 1 if n = 0, and n! = n × (n − 1)! if n > 0. Without a base case, or if the calls do not get smaller, the recursion never stops and the program fails with a stack overflow.
Definition: Recursion. A method of solving a problem in which a function calls itself on smaller instances of the same problem until a base case that can be solved directly is reached.
The Runtime Stack
Recursion is supported by the runtime (call) stack. Each call creates an activation record (stack frame) that holds its parameters, local variables and return address, and pushes it onto the stack. When the call returns, its frame is popped and control goes back to the caller. For fact(3), the frames for fact(3), fact(2), fact(1) and fact(0) are pushed in turn, and then popped as each returns, giving 1, 1, 2 and 6. The depth of the stack equals the depth of the recursion, so a recursion depth of n uses O(n) memory.
Applications of Recursion
Factorial: fact(n) = n × fact(n − 1), with fact(0) = 1. GCD by Euclid's algorithm: gcd(a, b) = a if b = 0, else gcd(b, a mod b). Fibonacci series: fib(0) = 0, fib(1) = 1, fib(n) = fib(n − 1) + fib(n − 2). The simple recursive Fibonacci repeats work and takes exponential time, O(2ⁿ), so a loop or memoisation is better. Towers of Hanoi: move n discs from peg A to peg C using B, never placing a larger disc on a smaller one. Move n − 1 discs from A to B, move the largest from A to C, then move the n − 1 discs from B to C. It needs 2ⁿ − 1 moves.
Recursion and Iteration
Every recursive algorithm can be written with a loop and, if needed, an explicit stack. Recursion is shorter and closer to the mathematical definition, and it is natural for trees. Iteration uses less memory and avoids the overhead of calls.
Table 3.4: Recursion and iteration
| Feature | Recursion | Iteration |
|---|---|---|
| Control | Function calls itself | Loop |
| Termination | Base case | Loop condition |
| Memory | Stack frame per call | Fixed |
| Code | Short, natural for trees | Often longer |
Example 3.4: Towers of Hanoi with 3 discs
Problem: List the moves to transfer 3 discs from peg A to peg C using peg B.
Solution:
- Move 2 discs from A to B: A→C, A→B, C→B.
- Move the largest disc: A→C.
- Move 2 discs from B to C: B→A, B→C, A→C.
Result: Seven moves: A→C, A→B, C→B, A→C, B→A, B→C, A→C, which is 2³ − 1.
#include <stdio.h>
long fact(int n) { return n == 0 ? 1 : n * fact(n - 1); }
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int fib(int n) { return n < 2 ? n : fib(n - 1) + fib(n - 2); }
void hanoi(int n, char a, char b, char c) {
if (n == 0) return;
hanoi(n - 1, a, c, b);
printf("%c->%c ", a, c);
hanoi(n - 1, b, a, c);
}
int main(void) {
printf("5! = %ld, gcd(48, 18) = %d\n", fact(5), gcd(48, 18));
for (int i = 0; i < 10; i++) printf("%d ", fib(i));
printf("\n");
hanoi(3, 'A', 'B', 'C');
printf("\n");
return 0;
}
Output
5! = 120, gcd(48, 18) = 6
0 1 1 2 3 5 8 13 21 34
A->C A->B C->B A->C B->A B->C A->C
Common mistakes
- Leaving out the base case, which causes infinite recursion and stack overflow.
- Making a recursive call that does not reduce the problem.
- Using plain recursive Fibonacci for large n, which is very slow.
Key takeaways
- A recursive function needs a base case and a smaller recursive case.
- Each call pushes a frame on the runtime stack.
- Factorial, GCD, Fibonacci and Towers of Hanoi are classic recursive problems.
- Towers of Hanoi with n discs needs 2ⁿ − 1 moves.
Practice questions
- What are the two parts of a recursive definition?
- What is stored in an activation record?
- How many moves does the Towers of Hanoi need for 4 discs?
Answers to the practice questions
- The base case and the recursive case.
- The parameters, local variables and return address of a call.
- 2⁴ − 1 = 15.
Sources
- Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit III
