Tail recursion refers to recursive call at last line. Tail

recursion can be eliminated by changing the recursive call to a goto preceded

by a set of assignments per function call. This simulates the recursive call

because nothing needs to be saved after the recursive call finishes. We can

just goto the top of the function with the values that would have been used in

a recursive call.

recursion can be eliminated by changing the recursive call to a goto preceded

by a set of assignments per function call. This simulates the recursive call

because nothing needs to be saved after the recursive call finishes. We can

just goto the top of the function with the values that would have been used in

a recursive call.

The recursive function for Tower of Honoi Problem is given

below. It is an example of recursive function with tail recursion.

below. It is an example of recursive function with tail recursion.

**Also Read: C Program for Tower of Hanoi Problem**

### Recursive function TOH with tail recursion

*void TOH(int n,char x,char y,char z)*

*{*

*if(n>0)*

*{*

*TOH(n-1,x,z,y);*

*printf(“n%c*

-> %c”,x,y);

-> %c”,x,y);

*TOH(n-1,z,y,x); //tail recursion*

*}*

*}*

### Without tail recursion

*void TOH(int n,char x,char y,char z)*

*{*

*char*

temp;

temp;

*label:*

*if(n>0)*

*{*

*TOH(n-1,x,z,y);*

*printf(“n%c*

-> %c”,x,y);

-> %c”,x,y);

*temp=x;*

*x=z;*

*z=temp;*

*n=n-1;*

*goto*

label;

label;

*}*

*}*

Watch below video to understand tail recursion easily.

*Image source: http://learnyousomeerlang.com/recursion*