C/C++ Algorithms

Recursion and the Call Stack in C and C++

A function that calls itself: see how each call waits on the stack until the base case returns.

  • Intermediate
  • 2 min read
  • 2 runnable examples
What you'll learn
  • Base case and recursive case
  • How calls stack up and unwind
  • When to use a loop instead
factorial(4) on the call stack
  1. factorial(1)base casereturns 1
  2. factorial(2)returns 2 * 1 = 2
  3. factorial(3)returns 3 * 2 = 6
  4. factorial(4)returns 4 * 6 = 24
Calls pile up from the bottom; results return from the top.

Each call waits for the next

The problem

Some problems are defined in terms of smaller versions of themselves: the factorial of 6 is 6 times the factorial of 5; a folder's size is its files plus its subfolders. Recursion lets a function solve such problems by calling itself.

Think of it like this

Recursion is like a stack of Russian nesting dolls. To reach the smallest doll, you open one doll, then the doll inside, and so on, until you reach the one that does not open (the base case). Then you put them back together in reverse order.

From nesting dolls to code
factorial(6)🪆 Open the biggest doll
number * factorial(number - 1)🔽 Open the next, smaller doll
number < 2 ? 1🛑 The smallest doll: does not open
720🔼 Close them all again: the answer
Each call waits for a smaller call until the base case returns.
In one sentence

A recursive function solves a problem by calling itself on a smaller version of it, until a base case is answered directly. Each pending call uses space on the call stack.

Important points

  1. number < 2 ? 1 is the base case: it returns without another call.
  2. number * factorial(number - 1) calls the function with a smaller number each time.
  3. factorial(6) makes six calls that wait on the stack, then multiply back to 720.
  4. In C++, constexpr lets the compiler compute factorial(6) before the program runs.

When to use it

  • Problems defined in terms of smaller ones: factorial, Fibonacci, power.
  • Tree and graph traversal: folders, menus, syntax trees.
  • Divide-and-conquer algorithms such as merge sort and quick sort.

When not to use it

  • Very deep recursion, such as a million levels: the call stack overflows; use a loop.
  • Simple counting loops, where recursion only adds overhead.

See how it works

Press Step to follow each call. New calls go onto the call stack, and each one returns its value to the call that made it.

C and C++ code examples

C
Run code →
main.c
#include <stdio.h>

unsigned long long factorial(unsigned number) {
    return number < 2 ? 1 : number * factorial(number - 1);
}

int main(void) {
    printf("%llu\n", factorial(6));
}
Get a Run button for this example Result of running the code
720
C++
Run code →
main.cpp
#include <iostream>

constexpr unsigned long long factorial(unsigned number) {
    return number < 2 ? 1 : number * factorial(number - 1);
}

int main() {
    constexpr auto value = factorial(6);
    std::cout << value << '\n';
}
Get a Run button for this example Result of running the code
720

C and C++ comparison

factorial(6) cannot answer directly, so it calls factorial(5), which calls factorial(4), and so on down to factorial(1). That call hits the base case and returns 1. Each waiting call then multiplies by its own number on the way back: 2, 6, 24, 120 and finally 720.

C

Loops, pointers, lengths, and temporary buffers are written explicitly.

C++

Iterators and standard algorithms separate operations from container representation.

Practice exercises

Run both versions, then modify them to observe the different language guarantees.

  • Trace each comparison on paper.
  • Test duplicates and extreme values.
  • Benchmark the custom implementation against the standard library.

Common mistakes

No base case

Without a stopping condition the function calls itself until the stack overflows.

Problem
return number * factorial(number - 1);
Fix
return number < 2 ? 1 : number * factorial(number - 1);

Not moving toward the base case

Calling with the same value never ends.

Problem
return number * factorial(number);
Fix
return number * factorial(number - 1);

Finished this lesson?

Mark it as learned to track your progress.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges