1.2.39 · D1Introduction to Programming (Python)

Foundations — Recursion depth limit — stack overflow

1,693 words8 min readBack to topic

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.


0. What is a function?

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).

We need this because recursion is nothing but a machine that presses its own button.


1. What is a variable / argument?

Figure — Recursion depth limit — stack overflow

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.


2. What does "the function calls itself" mean — recursion?

Picture two mirrors facing each other: each reflection contains a smaller copy of the same scene, again and again.


3. What is a stack (the shape, not the memory yet)?

Figure — Recursion depth limit — stack overflow

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.


4. What is a stack frame?

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.


5. What is the call stack and recursion depth?

Figure — Recursion depth limit — stack overflow

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.


6. Where do the frames physically live — stack memory vs heap?


7. What is sys and sys.getrecursionlimit()?


8. What is a RecursionError and try / except?


9. One extra tool you'll meet — tail recursion


Prerequisite map

Function and calling

Arguments and variables

Recursion calls itself

Stack frame sticky note

Stack LIFO pile

Call stack

Recursion depth height

Stack memory small and fixed

Recursion limit 1000

RecursionError catchable

Exceptions and try except

Recursion depth limit stack overflow

Return to the parent: parent topic.


Equipment checklist

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.