QamoosTech
Programming FundamentalsRuntime and memoryIntermediate

Tail Call

PronunciationTAYL kawl

Definition

A tail call is a function call that is the very last thing a function does. Some languages optimise it to reuse the current stack frame, so deep recursion doesn't overflow the stack.

Where you hear it

In functional programming (Scheme, Haskell, Erlang), recursion discussions and "why doesn't Python optimise this?" questions.

Examples

  • This recursion is a tail call, so Erlang runs it in constant stack space.
  • Python doesn't do tail-call optimisation; use a loop.
  • The tail-recursive version reuses one stack frame, so a million steps do not overflow.

Common mistake

Assuming every language optimises tail calls. Python and most mainstream languages do not.

Don't confuse with

Plain recursion, where work remains after the call returns (like n * factorial(n-1)), so the frame can't be dropped.

Say it at work

  • In a meeting or chatIs that a real tail call?
  • In an email or ticketRewrite it with an accumulator to make it a tail call.