devxlogo

Tail Recursion

Tail Recursion

Tail recursion is a special way of writing recursion in C/C++, which minimizes the time and memory, this can be done just by storing the value of variable in some temporary memory instead of depending on stack for it. Here’s an example:

 /* Factorial with Normal Recursion */int fact(int n){  if(n

When run with 4 as value of n fact(4)->4*Fact(3)->3*fact(2)->2*fact(1)->1 1->2*1->3*2->4*6=24

 /* Factorial with Tail Recursion */int fact(int n,int a){  if(n

When called with fact(4,1)fact(4,1)->fact(3,4)->fact(2,12)->fact(1,24)->24.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist