1.2.37 · D1Introduction to Programming (Python)

Foundations — Recursion — call stack visualization, base case, recursive case

1,793 words8 min readBack to topic

Before you can read the parent note, you need to own every piece of notation it throws at you. This page starts from absolute zero and builds each symbol on the one before it. Nothing is used before it is drawn.


1. What is a function? (the box that eats and returns)

Look at the figure: the input goes in the left side, the machinery churns, and exactly one value comes out the right side (the return value).

Figure — Recursion — call stack visualization, base case, recursive case

In Python this box is written:

def double(x):      # 'def' = "define a box named double that eats x"
    return x + x    # hand back x + x, then stop
  • def ::: the keyword that starts the box definition.
  • double ::: the box's name — how you call it later.
  • (x) ::: the parameter, a placeholder for whatever input you pass in.
  • return x + x ::: compute x + x, then send it back out and stop.

See Functions and return values for the full story of inputs and outputs.


2. Calling a function (and calling itself)

A function is even allowed to call itself with a different input:

def f(n):
    return f(n - 1)   # f calls f — this is recursion (careful: no stop here!)

That line — a box using its own name inside itself — is the seed of recursion. It only becomes safe once we add a stopping rule (Section 6).


3. The symbol n and the operation n - 1

Look at the number line in the figure: each recursive call slides the marker one place left, marching toward the special stopping value.

Figure — Recursion — call stack visualization, base case, recursive case

4. Lists and the slice L[0], L[1:]

The parent's second example uses lists, so we define them from zero.

The colon : means "and all that follows." The figure shows the list split into head (L[0], one item) and tail (L[1:], the leftovers).

Figure — Recursion — call stack visualization, base case, recursive case

5. The factorial symbol n!

The parent's main derivation uses n!, so let's earn that symbol.


6. Base case vs recursive case (the two mandatory halves)

Now the two starring terms of the whole topic.

The figure shows the fork every recursive function contains: a question at the top ("is the input tiny?"), a stop-and-answer branch (base case), and a shrink-and-call-again branch (recursive case).

Figure — Recursion — call stack visualization, base case, recursive case

7. The call stack: LIFO, frames, RecursionError

See The Call Stack and Stack Frames for the memory-level picture, and Big-O and Recursion Depth for how deep is too deep.


Prerequisite map

Function - box that eats input

return - hand a value back

Call - run with an input

Call itself - recursion seed

n minus 1 - shrink by one

Recursive case

List slice L1 onward - the rest

Factorial n bang - self similar product

Base case - known answer stops it

Call stack - pile of frames LIFO

Recursion - the topic

Each foundation feeds the next: a function that can return and call itself, plus a way to make the input smaller (n-1, L[1:]), plus a known stopping value (base case), all managed by the call stack.


Equipment checklist

Test yourself — cover the right side and answer before revealing.

What does return do inside a function?
Hands one value back to the caller and immediately stops that function.
If a function has no return, what value comes out?
None (the empty "nothing" value).
What is a "call"?
Actually running a function with a specific input.
What does n - 1 guarantee in a recursive call?
The input gets one step smaller, so the recursion moves toward its stopping point.
For the list [4, 2, 7], what is L[0] and what is L[1:]?
L[0] is 4 (the head); L[1:] is [2, 7] (the rest/tail).
What does n! mean, and what is 0!?
The product 1·2·3···n; and 0! is defined as 1.
State the self-similar rule for factorial.
n! = n · (n-1)!.
What is a base case?
The smallest input whose answer is known directly, with no further recursive call — it stops the recursion.
What is a recursive case?
The branch that calls the function on a smaller input and combines that result.
What is a stack frame, and do two calls share n?
A private notepad for one call holding its own locals; no, each call has its own independent n.
What does LIFO mean for the call stack?
Last In, First Out — the most recently pushed (deepest) frame finishes and pops first.
What error appears when there is no working base case?
RecursionError: maximum recursion depth exceeded.

Ready? Head back to the parent: the main note builds fact and traces the stack using exactly these pieces. Prefer loops instead? Compare with Iteration — for and while loops.