3 ms·
Tail recursion means that there's nothing in the continuation of the recursive call besides a return from the outer call. This means that a compiler can optimi
by bitwize 16y ago
Tail recursion means that there's nothing in the continuation of the recursive call besides a return from the outer call.
This means that a compiler can optimize away the saving of a new stack frame for a recursive call, because the old stack frame of the enclosing call will never be used again. That's tail-call optimization.
If there's stuff in between the return from the recursive call and the return from the enclosing call (like the multiply in this factorial example) then the old stack frame will have to be kept around which means you must use unoptimized recursive calls.