Open In App

What is a Proper Tail Call?

Last Updated : 21 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

What is a Proper Tail Call?

Proper tail calls (PTC) is a programming language feature that enables memory-efficient recursive algorithms. Tail call optimization is where you can avoid allocating a new stack frame for a function because the calling function will simply return the value it gets from the called function. The most common use is tail-recursion, where a recursive function written to take advantage of tail-call optimization can use constant stack space.

Proper Tail Call optimization means you can call a function from another function without increasing the call stack.

  • Programs that use Proper Tail Call may experience a low memory footprint because the garbage collector is more likely to collect certain local objects.
  • It  Reduces the stack usage, thus reducing the amount of cache space needed, freeing up cache space for other memory accesses.

Example 1: Finding the Greatest common divisor of two numbers using tail recursion

C++




// C++ code for the above example
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the Greatest common divisor
int gcd(int a, int b)
{
    if (b == 0) {
        return a;
    }
 
    // Tail Recursion
    return gcd(b, a % b);
}
 
// Driver code
int main()
{
    int a = 4, b = 8;
 
    // Calling Function and Printing
    cout << gcd(a, b);
    return 0;
}


Java




// Java code to implement the above approach
 
import java.io.*;
import java.util.*;
 
class GFG
{
// Function to calculate the Greatest common divisor
static int gcd(int a, int b)
{
    if (b == 0) {
        return a;
    }
  
    // Tail Recursion
    return gcd(b, a % b);
}
 
// Driver code
public static void main(String[] args)
{
    int a = 4, b = 8;
  
    // Calling Function and Printing
    System.out.println( gcd(a, b));
}
}
 
// This code is contributed by sanjoy_62.


C#




// C# program for above approach:
using System;
class GFG {
 
// Function to calculate the Greatest common divisor
static int gcd(int a, int b)
{
    if (b == 0) {
        return a;
    }
  
    // Tail Recursion
    return gcd(b, a % b);
}
   
// Driver Code
public static void Main()
{
    int a = 4, b = 8;
  
    // Calling Function and Printing
    Console.Write( gcd(a, b));
}
}
 
// This code is contributed by code_hunt.


Javascript




<script>
//Javascript code for the above example
 
 
// Function to calculate the Greatest common divisor
function gcd(a,b)
{
    if (b == 0) {
        return a;
    }
 
    // Tail Recursion
    return gcd(b, a % b);
   }
 
// Driver code
 
    let a = 4;
    let b = 8;
 
    // Calling Function and Printing
   document.write(gcd(a, b));
    
    // This code is contributed by satwik4409.
    </script>


Python3




def gcd(a, b):
    if b == 0:
        return a
    # Tail Recursion
    return gcd(b, a % b)
 
# Driver code
if __name__ == "__main__":
    a = 4
    b = 8
    # Calling Function and Printing
    print(gcd(a, b))
#This code is contributed by Edula Vinay Kumar Reddy


Output

4

Time Complexity: O(log(max(a, b)))
Auxiliary Space: O(1)

Example 2:  Program to calculate the multiplication of a number with two using Tail recursive

C++




#include <bits/stdc++.h>
using namespace std;
 
// Function to multiply value with 2
int Multi_Two(int value)
{
    int result = 0;
    for (int i = 0; i < value; ++i) {
        result += 2;
    }
 
    // Tail Recursive
    return result;
}
 
// Driver code
int main()
{
    int N = 34;
 
    // Calling Function and Printing
    cout << Multi_Two(N);
    return 0;
}


Java




import java.util.*;
   
class Main {
       
    // Function to multiply value with 2
    static int Multi_Two(int value)
    {
        int result = 0;
        for (int i = 0; i < value; ++i) {
            result += 2;
        }
   
        // Tail Recursive
        return result;
    }
       
    // Driver code
    public static void main(String args[])
    {
        int N = 34;
   
        // Calling Function and Printing
        System.out.println(Multi_Two(N));
    }
}
 
//This code is contributed by NarasingaNikhil


Python




# python program for above approach
 
# Function to multiply value with 2
 
def Multi_Two(value):
    result = 0
    for i in range(value):
        result += 2
    return result
 
N = 34
 
# Calling Function and Printing
print(Multi_Two(N))
 
#This code is contributed by NarasingaNikhil


C#




// C# program for above approach
using System;
class GFG {
    // Function to multiply value with 2
    static int Multi_Two(int value)
    {
        int result = 0;
        for (int i = 0; i < value; ++i) {
            result += 2;
        }
 
        // Tail Recursive
        return result;
    }
 
    // Driver code
    public static void Main()
    {
        int N = 34;
 
        // Calling Function and Printing
        Console.Write(Multi_Two(N));
    }
}
//This code is contributed by NarasingaNikhil


Javascript




<script>
 
// Function to multiply value with 2
function Multi_Two(value)
{
    let result = 0;
    for (let i = 0; i < value; ++i) {
        result += 2;
    }
 
    // Tail Recursive
    return result;
}
 
// Driver code
 
    let N = 34;
 
    // Calling Function and Printing
    document.write(Multi_Two(N));
     
    // This code is contributed by satwik4409.
    </script>


Output

68

Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads