1. Gyanastra
  2. Data Structures
  3. Recursion and the Call Stack
  4. Tracing Recursion with the Stack and Activation Records
Free lesson, no account needed 4 min read 19 practice questions after it
Asked in past papers29 times
  • GATE CSE20262025202420202018201720162015201420132008200720041999
  • ISRO202020182017201120082007
  • UGC NET202020182017201620152014201320092007

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. main gets one first; at the point where a(3) is called, an activation record for a(3) is created.
  • Pass by value: C copies the value of the actual argument into the formal parameter, so a(3) gives n = 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.
Data structure diagram: Tracing Recursion with the Stack and Activation Records
Figure. Tracing Recursion with the Stack and Activation Records

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:

  1. main creates its activation record; at the call a(3), an activation record for a(3) is pushed.
  2. Inside a(3), the formal parameter n gets the value 3 (pass by value).
  3. Check if (n > 0) → 3 > 0 is true.
  4. Execute printf("%d", n - 1) → prints 2.
  5. Call a(n - 1) = a(2).

Activation records for a(3)

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:

StepCall / Actionn valueCondition n > 0PrintedNext action
1main creates activation record, calls a(3)———push activation record for a(3)
2a(3) activation record createdn = 33 > 0 true—execute printf
3printf("%d", n-1)n = 3—2call 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
Pages 1–2 of 2