Computer Science I / Recursion and Iterative Alternatives
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
  1. Ask what the current frame is still needed for, if the recursive call's result is returned directly.
  2. 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.

Read the lesson: Recursion and Iterative Alternatives →

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