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?
f(n, total) = f(n − 1, total + n) does nothing after calling itself: the addition already happened. With n = 3: f(3, 0) → f(2, 3) → f(1, 5) → 6.
0/1
â–¶ Play