Data Structures · Recursion and the Call Stack
Tracing Recursion with the Stack and Activation Records
Overview
Recursion is a technique where a function calls itself. To understand how recursion actually executes, it is traced either with the stack data structure (the real mechanism) or with a tree method (a drawing shortcut); the choice depends on the type of question.
Key concepts
- Recursion: a function that calls itself.
- Stack method: tracing that shows how recursion actually works in practice; the stack is the data structure used for recursion.
- Tree method: tracing recursion without the stack.
- Activation record: created whenever any function is called.
maingets one first; at the point wherea(3)is called, an activation record fora(3)is created. - Pass by value: C copies the value of the actual argument into the formal parameter, so
a(3)givesn = 3. - Return point: when functions keep calling and activation records keep getting pushed, the point from which to resume a function on return must be remembered; the instruction pointer handles this.

How it works
The smallest recursive program used to demonstrate tracing:
void a(int n) // line 1
{ // line 2
if (n > 0) // line 3
{
printf("%d", n - 1); // line 4
a(n - 1); // line 6
}
}
a calls a again, so it is recursive. Trace of a(3) called from main:
maincreates its activation record; at the calla(3), an activation record fora(3)is pushed.- Inside
a(3), the formal parameterngets the value 3 (pass by value). - Check
if (n > 0)→ 3 > 0 is true. - Execute
printf("%d", n - 1)→ prints2. - Call
a(n - 1)=a(2).
Activation records for a(3)
Drawing the diagram…
Shows main's activation record and the push of a(3)'s record, which prints 2 and then calls a(2).
Worked examples
Trace of a(3) called from main:
| Step | Call / Action | n value | Condition n > 0 | Printed | Next action |
|---|---|---|---|---|---|
| 1 | main creates activation record, calls a(3) | — | — | — | push activation record for a(3) |
| 2 | a(3) activation record created | n = 3 | 3 > 0 true | — | execute printf |
| 3 | printf("%d", n-1) | n = 3 | — | 2 | call a(n-1) = a(2) |
Output so far: 2.
Revision summary
- The stack is the data structure used for recursion; the tree method is the alternative way to trace.
- Every function call creates an activation record on the stack.
- C uses pass by value: the actual argument's value is copied into the formal parameter (
a(3)→n = 3). - When activation records are pushed repeatedly, the return point (where to resume) must be remembered — the instruction pointer handles this.
- Shortcut: start tracing with the smallest recursive programs before attempting large recursion problems.
Previous-year exam insights
- Recursion depth equals the maximum number of simultaneously live activation records; each call pushes one frame and each return pops one, so the stack size needed is proportional to the maximum recursion depth (plus the caller frames).
Asked inGATE 2017GATE 2024 - A static local variable in a recursive function is allocated once and shared by all invocations, so its value persists and accumulates across the whole recursion; non-static locals are fresh in each activation record.
Asked inGATE 2020GATE 2016 - Infinite recursion (no base case reached, or the argument not moving toward the base case) causes stack overflow / abnormal termination, whereas a loop whose condition never becomes false causes an infinite loop — distinguish the two when tracing.
Asked inGATE 2017 - Code after a recursive call executes in reverse (LIFO) order on unwinding, so a function that prints before and after the recursive call produces output that is not simply the forward order.
Asked inGATE 2017GATE 2015GATE 2024 - When an array or pointer is passed to a recursive call as p+1 with size n-1, the recursion processes successive suffixes of the array;
the base case (n≤1 or size==0) stops it.
Asked inGATE 2025GATE 2016 - Pass by reference lets the recursive call modify the caller's variable, so changes made deeper in the recursion are visible when the value is used after the call returns; pass by value does not.
Asked inGATE 2013