Tail recusion and tail recursive elimination

Rahul S
2 min readApr 10, 2023
src: GeeksforGeeks

Tail recursion and tail call elimination are two related concepts in computer science that are often used together to optimize recursive functions.

Tail Recursion:

Tail recursion is a specific form of recursion where the last operation of a function is a recursive call. This means that there are no operations to be performed after the recursive call, and the function can simply return the result of the recursive call without any additional computation.

Here’s an example of a tail-recursive function to calculate the factorial of a number:

def factorial(n, acc=1):
if n == 0:
return acc
else:
return factorial(n-1, acc*n)

In this example, the recursive call to factorial is the last operation in the function. When the function is called with a non-zero value for n, the function simply returns the result of the recursive call multiplied by the current value of acc. This is an example of tail recursion because there are no additional operations to be performed after the recursive call.

Tail recursion is important because it allows for the optimization of recursive functions. When a function is tail-recursive, it can be easily optimized by a compiler or interpreter to eliminate the use of the call stack, which can…

--

--