Tail call

From Wikipedia, the free encyclopedia

A tail call is a subroutine call just before the end of a subroutine.

In assembly language, this construct can be optimized away. For example, in 6502 assembly, if a subroutine looks like:

sub:
 ...
 jsr othersub
 rts

the last two lines can be optimized to a jump instruction:

jmp othersub

The subroutine othersub will then return to the return address of sub. If the call stack does not just contain the return address, but also parameters for the subroutine, it may need to be adjusted.

If a subroutine performs a tail call on itself, this is called tail recursion. This can be optimized to a form of iteration, an optimization performed by interpreters of functional programming languages.