Tail recursion is when the recursive call is the last thing the function does. python # Not tail recursive - must multiply after recursive call def factorial(n): if n == 0: return 1 return n * factorial(n - 1) # Tail recursive - recursive call is last def factorial_tail(n, accumulator=1): if n == 0: return accumulator return factorial_tail(n - 1, n * accumulator) Some languages optimize tail recursion to use constant stack space.
Python doesn't, but understanding this pattern helps when learning other languages. The pattern: pass the partial result as a parameter instead of waiting to compute it after the recursive call.