How recursive functions run on the call stack, how to trace their output and return values, and how to count calls and stack depth with recurrences, the way GATE asks it in C code.
A recursive function calls itself on a smaller input. It needs
a base case that returns without recursing, and
a recursive case that moves towards the base case.
Missing or unreachable base cases give infinite recursion and a stack overflow.
Each call gets its own activation record (stack frame) holding its parameters, local variables and return address. Frames are pushed on the call stack when a call starts and popped when it returns, so calls finish in last-in first-out order.
Local (auto) variables are separate for each call.
static and global variables are shared by all calls. A change made deep in the recursion is seen by every caller after it returns.
Stack depth is the longest chain of calls active at one time. Space used is , not . For f(n) calling f(n - 1) the depth is about ; for a function that halves it is about .
Tail recursion: the recursive call is the very last action, with nothing left to do after it returns (for example return f(n - 1, acc * n);). A compiler can turn it into a loop, so it needs only stack. return n * f(n - 1); is not tail recursive, because the multiplication happens after the call.
2. Tracing output and return values
Where the print sits decides the order:
A print before the recursive call runs on the way down: f(3) prints 3, 2, 1.
A print after the call runs on the way back up: 1, 2, 3.
Prints on both sides give a mirror: 3 2 1 1 2 3.
Method for any trace
Write the call with its arguments.
Go down until you reach a base case; write the value it returns.
Substitute returned values back up, finishing each pending expression.
For two recursive calls, draw the recursion tree: a node per call, children for the calls it makes. The output order is the order you visit nodes (a preorder walk for prints before the calls).
Classic patterns
Function
Computes
p(n / 2) then print n % 2
binary digits of , most significant first
n * f(n - 1), base 1
f(b, a % b), base b == 0 returns a
f(n - 1) + 1 if odd, f(n / 2) if even
number of 1 bits in
f(n - 1) + f(n - 1), base f(0) = 1
Careful with order of evaluation. In return g(n - 1) + s; with s static, C does not say whether s is read before or after the call, so such code has no single answer. Well-posed questions store the call's result first.
3. Counting calls with recurrences
Let be the number of calls made when the function is called once with (including that call).
One recursive call on : , so , linear.
Two calls on : , , so .
Naive Fibonacci, base cases :
which solves to , with . It grows like , : exponential, because the same subproblems are solved again and again.
Tower of Hanoi. Move disks aside, move the largest, move back on top: , , so moves.
Running time follows the same recurrences with the work per call in place of 1. Halving recursions such as give ; gives .
Memoisation (store each answer the first time) turns naive Fibonacci into calls. That is the bridge to dynamic programming.
Recursion tree of naive fib(4): C(4) = 2F(5) - 1 = 9 calls
4. Mutual recursion and recursion vs iteration
In mutual recursion two or more functions call each other, for example A calls B and B calls A. C needs a prototype of the second function before the first uses it. Trace it exactly like ordinary recursion: keep a stack of pending calls, noting which function and argument each one is, and what remains to be done after it returns.
Recursion vs iteration
Recursion
Iteration
State
in stack frames
in loop variables
Extra space
usually
Overhead
call and return per step
none
Natural for
trees, divide and conquer, backtracking
simple counting loops
Every recursive function can be rewritten without recursion by managing an explicit stack. Tail recursion becomes a simple loop with no stack at all.
Checking termination. Find a quantity that strictly decreases on each call and is bounded below (for example , or in Euclid's algorithm). If none exists for some input, the recursion may not stop. For instance f(n) calling f(n - 2) with base case n == 0 never stops for odd , while base case n <= 1 stops for every .
Formulas to remember
Calls, one recursive call
Calls, two calls on n − 1
Calls of naive FibonacciBase cases ;
Tower of Hanoi moves
Stack space
Common mistakes
Treating a static or global variable as if each call had its own copy.
Printing on the way down when the print is after the recursive call (or the reverse).
Counting only the leaves, or forgetting the first call, when asked for the number of calls.
Confusing stack depth with total calls: space is .
Calling return n * f(n - 1); tail recursive; work remains after the call returns.
Worked examples
Try each step yourself, then reveal the working.
Example 1: Prints on both sides of the call
numerical
What is printed?
void f(int n) {
if (n == 0) return;
printf("%d ", n);
f(n - 1);
printf("%d ", n);
}
/* called as f(3) */
Step 1: Go down, printing before each call.
Try it on paper first.
Example 2: Calls made by naive Fibonacci
numerical
How many calls to fib (including the first) does fib(5) make?
int fib(int n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
Step 1: Set up the call-count recurrence.
Try it on paper first.
Example 3: Recursion with a static variable
numerical
What does g(3) return?
int g(int n) {
static int s = 0;
if (n <= 0) return 0;
s += n;
int r = g(n - 1);
return r + s;
}
/* called once as g(3) */
Step 1: Go down and track s, which all calls share.
Try it on paper first.
Example 4: Tower of Hanoi move count
numerical
What is the minimum number of moves to transfer 6 disks in the Tower of Hanoi, and what is the maximum recursion depth of the standard recursive solution (counting the first call as depth 1)?
Step 1: Write the recurrence for moves.
Try it on paper first.
Previous year questions
14 official GATE CSE questions on this topic, newest first.
10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.
Mark as mastered
Loading your progress…
My revision notes
Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.