What is a Proper Tail Call?
Last Updated :
21 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int main()
{
int a = 4, b = 8;
cout << gcd(a, b);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int gcd( int a, int b)
{
if (b == 0 ) {
return a;
}
return gcd(b, a % b);
}
public static void main(String[] args)
{
int a = 4 , b = 8 ;
System.out.println( gcd(a, b));
}
}
|
C#
using System;
class GFG {
static int gcd( int a, int b)
{
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static void Main()
{
int a = 4, b = 8;
Console.Write( gcd(a, b));
}
}
|
Javascript
<script>
function gcd(a,b)
{
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
let a = 4;
let b = 8;
document.write(gcd(a, b));
</script>
|
Python3
def gcd(a, b):
if b = = 0 :
return a
return gcd(b, a % b)
if __name__ = = "__main__" :
a = 4
b = 8
print (gcd(a, b))
|
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;
int Multi_Two( int value)
{
int result = 0;
for ( int i = 0; i < value; ++i) {
result += 2;
}
return result;
}
int main()
{
int N = 34;
cout << Multi_Two(N);
return 0;
}
|
Java
import java.util.*;
class Main {
static int Multi_Two( int value)
{
int result = 0 ;
for ( int i = 0 ; i < value; ++i) {
result += 2 ;
}
return result;
}
public static void main(String args[])
{
int N = 34 ;
System.out.println(Multi_Two(N));
}
}
|
Python
def Multi_Two(value):
result = 0
for i in range (value):
result + = 2
return result
N = 34
print (Multi_Two(N))
|
C#
using System;
class GFG {
static int Multi_Two( int value)
{
int result = 0;
for ( int i = 0; i < value; ++i) {
result += 2;
}
return result;
}
public static void Main()
{
int N = 34;
Console.Write(Multi_Two(N));
}
}
|
Javascript
<script>
function Multi_Two(value)
{
let result = 0;
for (let i = 0; i < value; ++i) {
result += 2;
}
return result;
}
let N = 34;
document.write(Multi_Two(N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...