Practice question · Multiple choice
A recursive function that would need a million frames crashes in Python and runs fine in a language with tail-call optimisation. What does that optimisation do?
Hints
- Ask what the current frame is still needed for, if the recursive call's result is returned directly.
- A frame holds the state you must return to. What if there is nothing to return to?
Show the answer
B. Reuses the current stack frame, turning the recursion into a loop
Why
If nothing happens after the recursive call, the current frame has no remaining work and can be reused, so depth costs no memory. Scheme mandates it and Python deliberately declines, preferring readable stack traces, which is why deep recursion in Python is a design error rather than a style choice.
Practise Recursion and Iterative Alternatives
The app has 6 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.
More questions on Recursion and Iterative Alternatives
- The lesson's factorial function is called as factorial(3). Order the events by when they happen in time.
- Every recursive function can be rewritten as a loop, and every loop can be rewritten recursively. Why does…
- Select every statement the lesson supports about choosing recursion over iteration.
- A recursive function that has a base case cannot recurse forever.