C/C++ Arena

Recursion stacks up frames

Each call to factorial gets a new frame, so there are several ns alive at once, one per frame. The calls stack up until n is 1 (the base case), then each frame returns its result to the one below it and disappears.

Watch the stack grow to four frames of factorial, then shrink back to main.

#include <stdio.h>

int factorial(int n) {
    if (n <= 1) {
        return 1;
    }
    int rest = factorial(n - 1);
    return n * rest;
}

int main(void) {
    int result = factorial(4);
    printf("4! = %d\n", result);
    return 0;
}

Output:

4! = 24

From the lesson: Functions