"Recursion feels like magic or madness until you see it's just two promises: 'here's a case I can answer outright,' and 'every other case, I'll shrink toward that one.' Hold both and recursion is safe; drop either and it spirals."
A Function That Calls Itself
Recursion is a function that solves a problem by calling itself on a smaller version of the same problem. It sounds circular, and it would be — except for a piece that breaks the circle. Every correct recursion has exactly two parts:
- Base case: a problem small enough to answer immediately, with no further recursion. This is the floor that stops the descent.
- Recursive case: solve a smaller version (by calling yourself), then build this answer from it — always moving toward the base case.
Factorial is the classic: factorial(0) = 1 (base case), and factorial(n) = n × factorial(n−1) (recursive case, shrinking n toward 0). Each call peels off one layer until it hits the base, then the answers multiply back up.
The Two Ways It Breaks
Recursion fails in exactly two ways, and both are failures of the contract. No base case: the function never stops calling itself, the call stack fills, and Python raises RecursionError. No progress toward the base case: even with a base case, if the recursive call isn't actually smaller (you call f(n) instead of f(n−1)), you never reach the floor — same crash. So the contract is two-sided: there must be a base case, AND every recursive call must move measurably closer to it.
Recursion Is Induction, Running
If you've seen mathematical induction, recursion will feel familiar — they're the same idea. Induction proves a statement by establishing a base case (it holds for n=0) and an inductive step (if it holds for n−1, it holds for n). Recursion computes the same way: base case + 'assume the smaller call is correct, build on it.' That assumption is the leap of faith from the Trees track: when writing the recursive case, trust that factorial(n−1) already returns the right answer, and only worry about combining it. Don't trace the whole stack in your head — the base case and induction guarantee correctness.
Pippa's Confession
n instead of n−1 — no progress). Dad gave me a checklist I still run: "Point at your base case. Now point at the line that makes the input smaller." If I can't point at both, it's broken. Those two questions turned recursion from a source of mysterious crashes into something I could verify in five seconds.