What is recursion
Recursion is when a function calls itself with a smaller input, solving a problem by breaking it into identical sub-problems. Each call creates a new stack frame that stores local variables. When the base case is reached, the stack unwinds — each frame returns its result to the caller. This unwinding is called backtracking.
- Base Case: The stopping condition — without it, the function recurses forever (stack overflow)
- Recursive Case: The function calls itself with a smaller/simpler input, moving toward the base case
- Call Stack: Each recursive call pushes a new frame; returning pops it — LIFO (Last In, First Out)
- Head Recursion: Work (print/compute) happens before the recursive call → output follows call order (1 → 10)
- Tail Recursion: Work happens after the recursive call → output follows return order (10 → 1) — compilers can optimize this
- Space Complexity: O(n) stack frames for depth n — each frame holds its own local variables
How This Visualizer Works
We use print(1 to 10) as the example. Each recursive call either prints before calling (Head Recursion) or prints after returning (Tail Recursion). Watch the call stack grow frame by frame and see how the output order flips completely — all without any tree or graph, just pure function calls.
1void print(int n) {
2if (n > 10) return; // base case
3printf("%d ", n); // ← print FIRST
4print(n + 1); // then recurse
5}
6
7// Output: 1 2 3 4 5 6 7 8 9 10
8print(1); // initial call
1void print(int n) {
2if (n > 10) return; // base case
3print(n + 1); // recurse FIRST
4printf("%d ", n); // ← print AFTER
5}
6
7// Output: 10 9 8 7 6 5 4 3 2 1
8print(1); // initial call
Head Recursion — Print 1 to 10
Stack empty — press Run or Step
1void print(int n) {
2if (n > 10) return;
3printf("%d", n); ← prints here
4print(n+1);
5} ← return
// printed values appear here
// return trace appears here
Tail Recursion — Print 10 to 1
Stack empty — press Run or Step
1void print(int n) {
2if (n > 10) return;
3print(n+1); ← recurse first
4printf("%d", n); ← prints on return
5} ← return
// printed values appear here
// return trace appears here
Head vs Tail — Side-by-Side Comparison
·printf BEFORE recursive call
·Output during call phase (going deeper)
·Output order: 1 2 3 4 5 6 7 8 9 10
·Stack grows → prints → stack shrinks
·Natural count-up order
·Not tail-call optimizable (print before)
·printf AFTER recursive call
·Output during return phase (unwinding)
·Output order: 10 9 8 7 6 5 4 3 2 1
·Stack grows fully → shrinks → prints
·Reversed order — unwinding effect
·Can be optimized to iteration by compiler
💡 The Key Insight — Why Order Flips
Imagine stacking 10 plates. In head recursion, you label each plate as you stack it — so the labels read 1, 2, 3… in stacking order. In tail recursion, you label each plate as you unstack it — so the labels read 10, 9, 8… in reverse.
The call stack is the same in both cases — 10 frames deep. What changes is when the work (printf) executes: before or after the recursive call returns.