# Tail Call Elimination

We have discussed (in tail recursion) that a recursive function is tail recursive if recursive call is the last thing executed by the function.

`// An example of tail recursive function ` `void` `print(` `int` `n) ` `{ ` ` ` `if` `(n < 0) ` ` ` `return` `; ` ` ` `cout << ` `" "` `<< n; ` ` ` ` ` `// The last executed statement is recursive call ` ` ` `print(n-1); ` `} ` |

*chevron_right*

*filter_none*

We also discussed that a __tail recursive is better than non-tail recursive as tail-recursion can be optimized by modern compilers. Modern compiler basically do tail call elimination to optimize the tail recursive code.__

If we take a closer look at above function, we can remove the last call with goto. Below are examples of tail call elimination.

`// Above code after tail call elimination ` `void` `print(` `int` `n) ` `{ ` `start: ` ` ` `if` `(n < 0) ` ` ` `return` `; ` ` ` `cout << ` `" "` `<< n; ` ` ` ` ` `// Update parameters of recursive call ` ` ` `// and replace recursive call with goto ` ` ` `n = n-1 ` ` ` `goto` `start; ` `} ` |

*chevron_right*

*filter_none*

**QuickSort : One more example**

QuickSort is also tail recursive (Note that MergeSort is not tail recursive, this is also one of the reason why QuickSort performs better)

`/* Tail recursive function for QuickSort ` ` ` `arr[] --> Array to be sorted, ` ` ` `low --> Starting index, ` ` ` `high --> Ending index */` `void` `quickSort(` `int` `arr[], ` `int` `low, ` `int` `high) ` `{ ` ` ` `if` `(low < high) ` ` ` `{ ` ` ` `/* pi is partitioning index, arr[p] is now ` ` ` `at right place */` ` ` `int` `pi = partition(arr, low, high); ` ` ` ` ` `// Separately sort elements before ` ` ` `// partition and after partition ` ` ` `quickSort(arr, low, pi - 1); ` ` ` `quickSort(arr, pi + 1, high); ` ` ` `} ` `} ` `// See below link for complete running code ` |

*chevron_right*

*filter_none*

The above function can be replaced by following after tail call elimination.

`/* QuickSort after tail call elimination ` ` ` `arr[] --> Array to be sorted, ` ` ` `low --> Starting index, ` ` ` `high --> Ending index */` `void` `quickSort(` `int` `arr[], ` `int` `low, ` `int` `high) ` `{ ` `start: ` ` ` `if` `(low < high) ` ` ` `{ ` ` ` `/* pi is partitioning index, arr[p] is now ` ` ` `at right place */` ` ` `int` `pi = partition(arr, low, high); ` ` ` ` ` `// Separately sort elements before ` ` ` `// partition and after partition ` ` ` `quickSort(arr, low, pi - 1); ` ` ` ` ` `// Update parameters of recursive call ` ` ` `// and replace recursive call with goto ` ` ` `low = pi+1; ` ` ` `high = high; ` ` ` `goto` `start; ` ` ` `} ` `} ` `// See below link for complete running code ` |

*chevron_right*

*filter_none*

Therefore job for compilers is to identify tail recursion, add a label at the beginning and update parameter(s) at the end followed by adding last goto statement.

**Next Article:**

QuickSort Tail Call Optimization (Reducing worst case space to Log n )

This article is contributed by **Dheeraj Jain**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

## Recommended Posts:

- QuickSort Tail Call Optimization (Reducing worst case space to Log n )
- Tail Recursion
- Tail Recursion for Fibonacci
- Tail recursion to calculate sum of array elements.
- Queries to find the count of characters preceding the given location
- Find the path from root to the given nodes of a tree for multiple queries
- Count number of steps to cover a distance if steps can be taken in powers of 2
- Minimum increment or decrement operations required to make the array sorted
- Queries to find the left-most given type integer in a binary array
- Blowfish Algorithm with Examples
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Minimum number of additons to make the string balanced
- Find a pair (n,r) in an integer array such that value of nPr is maximum
- Find a pair (n,r) in an integer array such that value of nCr is maximum