Skip to content
GATEiit

GATE CSE · Programming and Data Structures

Recursion

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.

Before you start

This topic comes before: Algorithm design techniques: greedy, dynamic programming and divide-and-conquer, Arrays, stacks, queues and linked lists, Asymptotic worst-case time and space complexity, Runtime environments, Trees, binary search trees and binary heaps

Concept notes

1. How a recursive call runs

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 O(depth), not O(number of calls). For f(n) calling f(n - 1) the depth is about n; for a function that halves n it is about log⁡2n.

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 O(1) 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

  1. Write the call with its arguments.
  2. Go down until you reach a base case; write the value it returns.
  3. 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 n, most significant first
n * f(n - 1), base 1 n!
f(b, a % b), base b == 0 returns a gcd⁡(a,b)
f(n - 1) + 1 if odd, f(n / 2) if even number of 1 bits in n
f(n - 1) + f(n - 1), base f(0) = 1 2n

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 C(n) be the number of calls made when the function is called once with n (including that call).

  • One recursive call on n−1: C(n)=C(n−1)+1, so C(n)=n+C(0), linear.
  • Two calls on n−1: C(n)=2C(n−1)+1, C(0)=1, so C(n)=2n+1−1.
  • Naive Fibonacci, base cases n≤1: C(n)=C(n−1)+C(n−2)+1,C(0)=C(1)=1, which solves to C(n)=2F(n+1)−1, with F(1)=F(2)=1. It grows like ϕn, ϕ≈1.618: exponential, because the same subproblems are solved again and again.

Tower of Hanoi. Move n−1 disks aside, move the largest, move n−1 back on top: M(n)=2M(n−1)+1, M(1)=1, so M(n)=2n−1 moves.

Running time follows the same recurrences with the work per call in place of 1. Halving recursions such as T(n)=T(n/2)+c give O(log⁡n); T(n)=2T(n/2)+cn gives O(nlog⁡n).

Memoisation (store each answer the first time) turns naive Fibonacci into O(n) calls. That is the bridge to dynamic programming.

fib(4)fib(3)fib(2)fib(2)fib(1)fib(1)fib(0)fib(1)fib(0)9 calls; fib(2) is computed twice
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 O(depth) usually O(1)
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 n, or b 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 n, while base case n <= 1 stops for every n≥0.

Formulas to remember

Calls, one recursive call
C(n)=C(n−1)+1⇒C(n)=n+C(0)
Calls, two calls on n − 1
C(n)=2C(n−1)+1,  C(0)=1⇒C(n)=2n+1−1
Calls of naive FibonacciBase cases n≤1; F(1)=F(2)=1
C(n)=2F(n+1)−1
Tower of Hanoi moves
M(n)=2n−1
Stack space
O(maximum recursion depth)

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 O(depth).
  • 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) */
  1. 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);
}
  1. 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) */
  1. 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)?

  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.

Practise these 14 PYQs →

Topic test

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.