Data Structures · Recursion and the Call Stack
Instruction Pointer and Resuming After a Recursive Call
Overview
When a recursive function returns, execution must continue at the correct point in the calling function.
To make this possible, every function call saves an instruction pointer (IP) in its activation record, alongside its local variables. This explains how recursion unwinds correctly and why recursive depth costs stack space.
Key concepts
- Instruction pointer (IP): the address or line number of the next statement to be executed in the calling function once control returns from the called (recursive) function.
- Activation record (stack frame): the block pushed on the stack for each active function call. It contains the function's local variables and the instruction pointer (the return point).
- For the traced function, each stack frame holds the local variable
nplus the instruction pointer.

How it works
The traced function, numbered by line for tracing purposes:
1 void A(int n)
2 {
3 if (n > 0)
4 {
5 printf("%d", n - 1);
A(n - 1); // the recursive call
6 } // end of function
}
Line numbers exist only to trace execution; line 6 marks the end of the function, and the recursive call A(n-1) sits between the printf and the end of the function.
A(3)is called (frommain).n = 3, son > 0is true. It executesprintf("%d", n-1)→ prints 2, then callsA(2).
AfterA(2)returns, the next statementA(3)must execute is line 5 (the end of the function); this return point is stored as the IP inA(3)'s activation record.A(2)is called.n = 2, son > 0is true.printf("%d", n-1)→ prints 1, then it callsA(1).
On return it must resume at line 5, so IP = line 5 is saved inA(2)'s activation record.
A new activation record is pushed on top.A(1)is called.n = 1, son > 0is true.printf("%d", n-1)→ prints 0, then it callsA(0). On return it must resume at line 5, so IP = line 5 is saved inA(1)'s activation record.A(0)is called.n = 0, son > 0is false;A(0)simply returns without doing anything (base case).- Unwinding begins:
A(0)returns to its callerA(1), the frame immediately below it on the stack.A(1)resumes at line 5, the end of the function →A(1)is over.A(1)returns toA(2).A(2)resumes at line 5 → end of function →A(2)is over.A(2)returns toA(3).A(3)resumes at line 5 → end of function →A(3)is over.A(3)returns tomain.mainalso saved an IP, somaincontinues from where it left off.
Output printed: 210
Stack contents during the deepest call (top of stack at the right):
| Frame (bottom → top) | Local variable n | Instruction pointer (resume line) |
|---|---|---|
| main | — | return point after calling A(3) |
| A(3) | 3 | line 5 |
| A(2) | 2 | line 5 |
| A(1) | 1 | line 5 |
| A(0) | 0 | — (returns immediately, base case) |
Formulas
- Number of activation records pushed to compute
A(n)(includingmain's frame): - Stack space required = → this is the space complexity
- Time complexity recurrence: , where = constant time for the check and the
printf, and = time for the recursive call - Solving the recurrence gives
Worked examples
Trace of A(3) with the numbered function above:
| Call | n | n > 0? | Prints (n−1) | Calls next | IP saved (resume line) |
|---|---|---|---|---|---|
| A(3) | 3 | yes | 2 | A(2) | line 5 |
| A(2) | 2 | yes | 1 | A(1) | line 5 |
| A(1) | 1 | yes | 0 | A(0) | line 5 |
| A(0) | 0 | no | — | returns | — |
Unwinding: A(0) → A(1) (line 5, ends) → A(2) (line 5, ends) → A(3) (line 5, ends) → main (continues).
Final output: 210
Stack size count: 1, 2, 3, 4, 5 → 5 activation records for the whole run. To compute A(3) itself, 4 stack elements are needed (A(3), A(2), A(1), A(0)). In general, computing A(n) needs stack elements = .
Complexity result: time complexity and space complexity are both .
Comparison
| Aspect | Value for this recursion |
|---|---|
| Time complexity | (from ) |
| Space complexity (stack) | ( activation records) |
| What each frame stores | local variable n + instruction pointer |
| Base case | n = 0 → returns immediately, prints nothing |
Revision summary
- The instruction pointer is the next statement to execute after returning from the recursive call; it is saved in the caller's activation record.
- Each activation record holds the local variables and the instruction pointer.
- The base case (
n = 0) returns without doing anything; control then goes to the frame immediately below on the stack. - For
A(n), activation records are pushed; stack space = . - Time recurrence → ; both time and space are .
- Exam tip: questions generally do not ask the exact number of records pushed; they ask for the order ().
- Shortcut: to count stack frames for
A(n), count the calls fromA(n)down toA(0)inclusive and addmain's frame — forA(3)that is 4 + 1 = 5.
Previous-year exam insights
- A recursive call that does not move its argument toward the base case (e.g. passing the same value, as with post-decrement
val--which passes the old value) causes infinite recursion and eventual stack overflow / abnormal termination; a call that decreases the argument (val - 1) terminates.
Asked inGATE 2017 - In a function of the form
if (x < K) x = f(x + d); return x - 1;, the deepest call (whose is the first value ≥ K reached by repeatedly adding d) returns - 1, and each of the k earlier frames assigns the returned value to x and subtracts 1 again, so the value finally returned is - 1 - k.
Asked inUGC NET 2017 - Recursion depth equals the number of nested active calls; each active call consumes one stack frame, so stack space (and hence space complexity) is proportional to the maximum recursion depth, not to the total number of calls made.
Asked inGATE 2017UGC NET 2017 - The saved return address (instruction pointer) in a frame points to the instruction immediately after the call site in the caller, so on return execution resumes there; if statements follow the recursive call, they execute during unwinding in reverse order of the calls.
- For a recurrence T(n) = T(n-1) + c with T(0) = constant, the solution is T(n) = ;
for T(n) = 2T(n-1) + c it is , so the number of recursive calls per level determines whether time is linear or exponential. - Tail recursion (recursive call is the last operation) can in principle reuse a single frame, but standard C/Java compilers need not do so; unless tail-call optimization is guaranteed, assume stack space for depth-n recursion.