1. Gyanastra
  2. Data Structures
  3. Recursion and the Call Stack
  4. Instruction Pointer and Resuming After a Recursive Call
Free lesson, no account needed 7 min read 11 practice questions after it
Asked in past papers2 times
  • GATE CSE2017
  • UGC NET2017

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 n plus the instruction pointer.
Data structure diagram: Instruction Pointer and Resuming After a Recursive Call
Figure. Instruction Pointer and Resuming After a Recursive Call

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.

  1. A(3) is called (from main). n = 3, so n > 0 is true. It executes printf("%d", n-1) → prints 2, then calls A(2).
    After A(2) returns, the next statement A(3) must execute is line 5 (the end of the function); this return point is stored as the IP in A(3)'s activation record.
  2. A(2) is called. n = 2, so n > 0 is true. printf("%d", n-1) → prints 1, then it calls A(1).
    On return it must resume at line 5, so IP = line 5 is saved in A(2)'s activation record.
    A new activation record is pushed on top.
  3. A(1) is called. n = 1, so n > 0 is true. printf("%d", n-1) → prints 0, then it calls A(0). On return it must resume at line 5, so IP = line 5 is saved in A(1)'s activation record.
  4. A(0) is called. n = 0, so n > 0 is false; A(0) simply returns without doing anything (base case).
  5. Unwinding begins:
    • A(0) returns to its caller A(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 to A(2). A(2) resumes at line 5 → end of function → A(2) is over.
    • A(2) returns to A(3). A(3) resumes at line 5 → end of function → A(3) is over.
    • A(3) returns to main. main also saved an IP, so main continues 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 nInstruction pointer (resume line)
main—return point after calling A(3)
A(3)3line 5
A(2)2line 5
A(1)1line 5
A(0)0— (returns immediately, base case)

Formulas

  • Number of activation records pushed to compute A(n) (including main's frame): n+1n + 1
  • Stack space required = O(n)O(n) → this is the space complexity
  • Time complexity recurrence: T(n)=c+T(n−1)T(n) = c + T(n - 1), where cc = constant time for the check and the printf, and T(n−1)T(n-1) = time for the recursive call
  • Solving the recurrence gives T(n)=O(n)T(n) = O(n)

Worked examples

Trace of A(3) with the numbered function above:

Callnn > 0?Prints (n−1)Calls nextIP saved (resume line)
A(3)3yes2A(2)line 5
A(2)2yes1A(1)line 5
A(1)1yes0A(0)line 5
A(0)0no—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 n+1n + 1 stack elements = O(n)O(n).

Complexity result: time complexity and space complexity are both O(n)O(n).

Comparison

AspectValue for this recursion
Time complexityO(n)O(n) (from T(n)=c+T(n−1)T(n) = c + T(n-1))
Space complexity (stack)O(n)O(n) (n+1n + 1 activation records)
What each frame storeslocal variable n + instruction pointer
Base casen = 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), n+1n + 1 activation records are pushed; stack space = O(n)O(n).
  • Time recurrence T(n)=c+T(n−1)T(n) = c + T(n-1) → O(n)O(n); both time and space are O(n)O(n).
  • Exam tip: questions generally do not ask the exact number of records pushed; they ask for the order (O(n)O(n)).
  • Shortcut: to count stack frames for A(n), count the calls from A(n) down to A(0) inclusive and add main's frame — for A(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 xnx_{n} is the first value ≥ K reached by repeatedly adding d) returns xnx_{n} - 1, and each of the k earlier frames assigns the returned value to x and subtracts 1 again, so the value finally returned is xnx_{n} - 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) = O(n)O(n);
    for T(n) = 2T(n-1) + c it is O(2n)O(2^{n}), 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 O(n)O(n) stack space for depth-n recursion.
Pages 1–2 of 2