Why is Tail Recursion optimization faster than normal Recursion?
Last Updated :
05 Jul, 2025
Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call.
Why is tail recursion optimization faster than normal recursion?
In non-tail recursive functions, after one recursive call is over, there are more statements to compute, and hence all the functions do not unfold as soon as the base case is hit.
Every time when a recursive call is done (or any function call), a stack frame is added to the call stack. This keeps track of where you were at the time the function call was made so we can continue from where we left off. If this is done enough times, say through a recursive function to compute the 1 billionth Fibonacci number, you can get a stack overflow, which will typically terminate the process.
Tail recursion works off the realization that some recursive calls don't need to "continue from where they left off" once the recursive call returns. Specifically, when the recursive call is the last statement that would be executed in the current context. Tail recursion is an optimization that doesn't bother to push a stack frame onto the call stack in these cases, which allows your recursive calls to go very deep in the call stack.
If the last action of a method is a call to another method, instead of creating a new stack frame for the context of the new method (arguments, local variables, etc.), we can replace the current one. One of the drawbacks of normal recursive methods is that they heavily use the call stack. Tail-call optimization eliminates this problem and guarantees that the performance of a recursive algorithm is exactly as good as its iterative counterpart (yet potentially it is much more readable).
Example of tail recursive function:
C++
// C++ code to implement the above approach
#include <bits/stdc++.h>
using namespace std;
// Tail recursion
void fun(int n)
{
if(n == 0)
return;
cout<<n<<" ";
fun(n-1);
}
// Driver Code
int main()
{
fun(5);
return 0;
}
// This code is contributed by Abhishek Thakur.
Java
// Java code to implement the approach
import java.util.*;
class GFG {
// Tail recursion
static void fun(int n)
{
if(n == 0)
return;
System.out.print(n + " ");
fun(n-1);
}
// Driver Code
public static void main (String[] args) {
fun(5);
}
}
// This code is contributed by sanjoy_62.
Python
# Tail recursion
def fun(n):
if(n == 0):
return
print(n, end =" ")
fun(n-1)
# Driver code
if __name__=="__main__":
fun(5)
C#
// C# code to implement the approach
using System;
public class GFG {
// Tail recursion
static void fun(int n)
{
if (n == 0)
return;
Console.Write(n + " ");
fun(n - 1);
}
static public void Main()
{
// Code
fun(5);
}
}
// This code is contributed by lokesh.
JavaScript
// JS code to implement the above approach
// Tail recursion
function fun(n)
{
if(n == 0)
return;
console.log(n);
fun(n-1);
}
// Driver Code
fun(5);
// This code is contributed by akashish__
Time Complexity: O(N)
Auxiliary Space: O(N)
Example of non-tail recursive function:
C++
#include <iostream>
using namespace std;
// Non-tail recursion
void fun(int n)
{
if (n == 0)
return;
fun(n - 1);
cout<<n<<" ";
}
int main() {
fun(5);
return 0;
}
// This code is contributed by akashish__
Java
// Java code to implement the approach
import java.util.*;
class GFG {
// Non-tail recursion
static void fun(int n)
{
if(n == 0)
return;
fun(n-1);
System.out.print(n + " ");
}
// Driver Code
public static void main (String[] args) {
fun(5);
}
}
// This code is contributed by Pushpesh Raj.
Python
# Non-tail recursion
def fun(n):
if(n == 0):
return
fun(n-1)
print(n, end =" ")
# Driver code
if __name__=="__main__":
fun(5)
C#
// C# code to implement the approach
using System;
public class GFG {
// Non-tail recursion
static void fun(int n)
{
if (n == 0)
return;
fun(n - 1);
Console.Write(n + " ");
}
static public void Main()
{
// Code
fun(5);
}
}
// This code is contributed by lokesh.
JavaScript
function fun(n) {
if (n === 0) return;
fun(n - 1);
console.log(n);
}
fun(5);
// This code is contributed by akashish__
Time Complexity: O(N)
Auxiliary Space: O(N)
Related Articles:
Similar Reads
Introduction to Recursion The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
What is Recursion? Recursion is defined as a process which calls itself directly or indirectly and the corresponding function is called a recursive function.Example 1 : Sum of Natural Numbers Let us consider a problem to find the sum of natural numbers, there are several ways of doing that but the simplest approach is
8 min read
Difference between Recursion and Iteration A program is called recursive when an entity calls itself. A program is called iterative when there is a loop (or repetition).Example: Program to find the factorial of a number C++#include<iostream> using namespace std; // ----- Recursion ----- // method to find // factorial of given number in
5 min read
Types of Recursions What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inord
15+ min read
Finite and Infinite Recursion with examples The process in which a function calls itself directly or indirectly is called Recursion and the corresponding function is called a Recursive function. Using Recursion, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Tr
6 min read
What is Tail Recursion Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call.For example the following function print() is tail recursive.C++// An example of tail recursive funct
7 min read
What is Implicit recursion? What is Recursion? Recursion is a programming approach where a function repeats an action by calling itself, either directly or indirectly. This enables the function to continue performing the action until a particular condition is satisfied, such as when a particular value is reached or another con
5 min read
Why is Tail Recursion optimization faster than normal Recursion? Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call.Why is tail recursion optimization faster than normal recursion?In non-tail recursive functions, afte
4 min read
Recursive Functions A Recursive function can be defined as a routine that calls itself directly or indirectly. In other words, a recursive function is a function that solves a problem by solving smaller instances of the same problem. This technique is commonly used in programming to solve problems that can be broken do
4 min read
Difference Between Recursion and Induction Recursion and induction are fundamental ideas in computer science and mathematics that might be regularly used to solve problems regarding repetitive structures. Recursion is a programming technique in which a function calls itself to solve the problem, whilst induction is a mathematical proof techn
4 min read