Intuition The ONE core idea
When a function calls itself, the computer stacks up a little "sticky note" for each call so it can find its way back — and those notes live in a small, fixed drawer that can fill up. Recursion depth limit is the topic of what happens when too many sticky notes pile up faster than they can be removed.
This page assumes you know nothing . Before we ever say the words "stack overflow", let us earn every single symbol, word, and picture the parent note leans on — one at a time, each building on the last.
A function is a named box of instructions you can run by writing its name with round brackets. Running it is called calling it.
Picture a vending machine with a label on it. You press the button (that is the call ), it does its job inside, and it hands you back something (the return value ).
Worked example The tiniest function
def greet ():
return "hi"
greet() # this line is a CALL; the machine hands back "hi"
def means "define a machine". greet is the machine's name. () is you pressing its button.
We need this because recursion is nothing but a machine that presses its own button.
A value you hand into a function through the brackets is an argument . Inside the function it lives under a name; that name is a variable — a labelled cup that holds one value.
In boom(0) from the parent note, 0 is the argument. Inside boom, the name n is a cup holding 0. When boom calls boom(n + 1), a brand-new cup named n is made holding 1. The two cups do not share — each call gets its own.
Intuition Why this matters for the whole topic
Because every call makes its own cups, ten stacked calls means ten separate sets of cups all sitting in memory at once. That "sitting in memory at once" is exactly what fills the drawer.
Recursion is when a function's instructions include calling itself . See Recursion — base case and recursive case for the two halves every good recursion needs.
Picture two mirrors facing each other: each reflection contains a smaller copy of the same scene, again and again.
Definition Base case & recursive case
The base case is the "stop here, don't call again" answer — the finish line.
The recursive case is the "call myself on a smaller problem" step.
Common mistake "A base case existing is enough."
Why it feels right: you wrote a stopping condition. Why it's wrong: if each call doesn't move toward it, you never arrive. f(n) calling f(n+1) with base n==0 sails away from the finish line forever.
A stack is a pile where you can only add to the top and remove from the top . Last thing In is the First thing Out — LIFO .
Think of a spring-loaded plate dispenser in a canteen. You push a plate on top; you pop the top plate off. You can never yank a plate from the middle.
Intuition Why a stack, not a queue or a list?
Function calls are perfectly nested: the last function you entered is always the first one that finishes and hands control back. That "last-in finishes first" pattern is exactly LIFO — so a stack is the natural bookkeeping tool. See Call stack and function frames .
A stack frame is one plate in the pile. It is the "sticky note" for a single active call, holding that call's arguments, local variables, and the return address (the exact line to jump back to when this call finishes).
Definition Return address
The return address is a bookmark: "when you're done, resume the caller here ." Without it the computer wouldn't know where it came from.
So the plate for boom(2) remembers: my n is 2, and when I'm done, go back into boom(1) right after the spot that called me.
The call stack is the actual stack of frames the interpreter keeps while your program runs — one frame pushed per call, popped when that call returns.
Definition Recursion depth
The recursion depth is how many frames are stacked at the same time — the height of the pile right now.
The key subtlety: in a recursive call like boom(n + 1), the inner call starts before the outer one returns. So no plate is ever popped — the pile only grows. Depth climbs by 1 with every call. Compare with Iteration vs Recursion , where a loop reuses a single frame and the pile stays height 1.
Stack memory is a small, fixed region of RAM (often just 1–8 MB) reserved for the call stack. The heap is a separate, much larger region for big data. See Memory model — stack vs heap .
Intuition Why the limit exists at all
Because stack memory is small and fixed , an ever-growing pile of frames eventually runs out of room. If the computer just kept pushing, the operating system would kill the program with an ugly, silent crash (a segfault ). To stop this, CPython counts frames and complains first .
sys and the recursion limit
sys is a built-in Python toolbox. sys.getrecursionlimit() reads the safety cap (default 1000 frames). sys.setrecursionlimit(N) changes it. The dot . means "reach inside sys and use the tool named after it".
Definition Exception & RecursionError
An exception is Python's way of saying "something went wrong" by raising a special object that interrupts the program. A ==RecursionError== is the specific one meaning "you passed the recursion limit." Because it's a normal exception, you can catch it with try / except. See Exceptions and try-except in Python .
Intuition Why "catchable" is the whole point
A segfault gives you no traceback and no second chance. A RecursionError is polite — you can wrap the risky call in try, catch the error in except, and recover gracefully. That is why Python chose a soft limit.
Definition Tail recursion
A recursion where the self-call is the very last thing the function does is tail recursive . Some languages optimize this into a loop (no growing stack). Python deliberately does not — see Tail recursion and why Python lacks tail-call optimization . So even tail-recursive Python still grows the stack and can overflow.
Stack memory small and fixed
Exceptions and try except
Recursion depth limit stack overflow
Return to the parent: parent topic .
Self-test: cover the right side and answer each.
What does calling a function mean, in the vending-machine picture? Pressing its button so it runs and hands something back.
Do two nested calls to the same function share their variable cups? No — each call gets its own fresh set of local cups.
What is recursion in one sentence? A function whose instructions include calling itself.
What are the two halves every recursion needs? A base case (stop) and a recursive case (call itself on a smaller problem).
What does LIFO stand for and why do calls fit it? Last In First Out; the last call entered is the first to finish and return.
What three things does a stack frame hold? The call's arguments, local variables, and return address.
What is recursion depth? The number of frames stacked at the same time — the height of the pile.
Why is stack memory dangerous for deep recursion? It is small and fixed (~1–8 MB), so a growing pile can run out of room.
What reads and sets the recursion limit? sys.getrecursionlimit() and sys.setrecursionlimit(N).
Why can't you use exactly 1000 of your own calls? The limit counts all frames, including ones already on the stack, so you get about 1000 minus those.
Why is RecursionError better than a segfault? It is catchable with try/except, so you can recover instead of crashing silently.
Does Python's tail-call recursion avoid stack growth? No — Python has no tail-call optimization, so it still grows the stack.