Base cases
Not met yet
A base case is the answer a recursive function gives straight away, without calling itself: an empty crate weighs nothing, a hook on the ground needs no pulls. Every call must move closer to a base case, and the base case has to catch every way the calls can arrive, not just the one you pictured. height == 0 misses a height that goes 3, 1, -1; height <= 0 doesn't. Without a base case that's reached, Python stops the program with a RecursionError.
Try it
main.pyOutput
Press Run and what your program prints shows up here.
Easy to get wrong
- Thinks the steps of a recursion will land exactly on the base case, when they can jump past it.
- Thinks a recursive function with no base case runs for ever like an infinite loop, rather than stopping with a RecursionError.
- Thinks the base case can be checked after the recursive call, rather than before it.