Skip to content
uencoder

Tail Recursion

Tail recursion is recursion where the recursive call is the function's final operation, which compilers can optimize into a loop to avoid stack overflow.

Purpose

Express loop logic with recursion without growing the stack — a core of functional languages.

Mechanism

If the recursive call is in tail position (the last operation before returning), TCO turns it into a jump instead of a call, reusing the stack frame. Requires language support (mandatory in Scheme/SML; optional in JS strict mode per spec, but only Safari/JavaScriptCore implements it). Non-tail recursion must be rewritten with an accumulator.

Use cases

  • Functional programming
  • Stack overflow avoidance
  • Recursion optimization

Related terms

References