Programmer's Wiki
Advertisement

A tail call is a language specific return value optimization, implemented mainly in functional programming languages. It works by destroying a function off of the stack as soon as it returns a function call. In simpler terms, when the return value is coming from another function being called, the compiler will shift work over to the proper procedure as to save time and memory.

Examples[]

Lua[]

-- a pointless function
function x()
    return 1;
end

-- another pointless function...
function y()
    return x(); -- since the return value is a function call, it will destroy 'y' and move to 'x'
end

y() -- calls 'y', but in reality, you're calling 'x'
16px-Pencil
Advertisement