Algorithms and Problem Solving · Lesson Supplemental
Recursion
A function that calls itself: base case to stop, recursive case to shrink, and factorial plus Fibonacci traced to the last call.
01
Learning objectives
Define recursion and identify the base case and recursive case in a function.
Trace factorial(4), showing the chain of calls and the returns.
Explain what happens when the base case is missing (RecursionError).
Trace fib(5) and point out where the same values are recomputed.
State the stack-depth idea: each waiting call occupies a frame until it can finish.
02
Lesson notes
Recursion is a function calling itself to solve a smaller copy of the same problem — like Russian dolls: open one, and a smaller one is inside. Every correct recursive function has exactly two parts. The base case answers the smallest problem directly and stops the process; the recursive case does one small piece of work and delegates the rest to another call with a reduced input.
Factorial is the canonical trace. factorial(n) returns 1 when n == 0 (base case), otherwise n * factorial(n - 1). Calling factorial(4) opens 4 × factorial(3), which opens 3 × factorial(2), and so on down to factorial(0) = 1. The multiplications then collapse on the way back: 1 × 1, then 2 × 1, then 3 × 2, then 4 × 6 = 24. The calls go down before any answer comes up.
Fibonacci shows both the power and the waste. fib(n) = fib(n-1) + fib(n-2) with base cases fib(0) = 0 and fib(1) = 1 mirrors the mathematical definition beautifully — but it recomputes the same values many times: fib(5) evaluates fib(3) twice and fib(2) three times. The tree of calls grows exponentially, which is why this elegant version becomes slow for n beyond roughly 35, even though it always returns correct answers.
Each waiting call occupies a frame on Python's call stack, and the stack has a limited depth — which is why a missing base case is fatal. def boom(n): return boom(n - 1) never stops: calls pile up until Python raises RecursionError: maximum recursion depth exceeded. So the first code-review question for any recursive function is: where exactly is the base case, and does every path reach it?
Board tip: exam questions ask you to show all the calls for factorial(4) or fib(4) — draw the cascade of calls downward, then write the returns upward beside each level. Check the base case first, before any arithmetic, because a wrong base case poisons every level of the trace. And remember fib(6) = 8 under the convention fib(0) = 0 — off-by-one conventions are marked strictly.
Recursive calls build a call stack
03
Worked code examples
Factorial: base case plus recursive case
factorial(5) waits on factorial(4), and so on down to factorial(0) = 1; the multiplications then unwind as 5 × 4 × 3 × 2 × 1 = 120.
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
print(factorial(0))
print(factorial(5))
Output
1
120
Fibonacci by definition
The two base cases (fib(0) = 0 and fib(1) = 1) stop the branching; the calls fan out and sum back up the tree to 8.
def fib(n):
if n <= 1:
return n
return fib(n - 1) + fib(n - 2)
print(fib(6))
Output
8
Recursion you can watch
Each call prints its own n before delegating the smaller problem; the base case prints the finale and stops the chain.
def countdown(n):
if n == 0:
print("Lift off!")
return
print(n)
countdown(n - 1)
countdown(3)
Output
3
2
1
Lift off!
04
Key terminology
recursion
A technique where a function calls itself on a smaller version of the problem.
base case
The smallest input, answered directly without further calls — what makes recursion stop.
recursive case
The part of the function that does one step of work and calls itself on reduced input.
call stack
The structure that records one frame per waiting function call.
RecursionError
The error Python raises when calls exceed the maximum stack depth — the signature of a missing base case.
05
Common misconceptions
Common mistake
A recursive function without a base case will still stop for small inputs.
Correct understanding
No base case means no stopping condition at any size; the calls pile up until RecursionError, whatever the starting n.
Common mistake
Recursion is always faster than a loop.
Correct understanding
Each call costs stack space and time; the double-recursion fib is far slower than a simple loop computing the same sequence.
Common mistake
factorial(0) is an error case the base clause merely tolerates.
Correct understanding
0! = 1 by definition — it is the intended base case that makes the whole recursion terminate correctly.
06
Check your understanding
1. def f(n): if n == 0: return 1; return n * f(n - 1) — what is f(4)?
2. With fib(0) = 0 and fib(1) = 1, what is fib(5)?
3. What is missing if a recursive function never reaches its base case?