Algorithms

Tail recursion

Recursion where the call to itself is the last thing the function does. Since nothing is pending, some compilers reuse the same frame and use memory like a loop.

Example

f(n, total) = f(n − 1, total + n): f(3, 0) → f(2, 3) → f(1, 5) → 6, adding on the way down.

Test yourself: Tail recursion

Iteration and recursion

What is tail recursion?

Lessons that use it

More algorithms terms

All terms →

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.