**Types of Recursion**

Recursive functions can be classified on the basis of :

a.) If the functions calls itself directly or indirectly. – Direct / Indirect

b.) If an operation is pending at each recursive call. – Tail Recursive/ Not

c.) based on the structure of function calling pattern. – Linear / Tree

a.) **Direct Recursion:**

- If a function explicitly calls itself it is called directly recursive.
- When the method invokes itself it is direct.
- Example:

int testfunc( int num)

{

if (num == 0)

return 0;

else

return (testfunc(num-1));

}

Here, the function ‘testfunc’ calls itself for all positive values of num.

__b.) Indirect Recursion:__

- This occurs when the function invokes other method which again causes the original function to be called again.
- If a method ‘X’ , calls method ‘Y’, which calls method ‘Z’ which again leads to ‘X’ being invoked is called indirect recursive or mutually recursive as well.
- Example:

int testfunc1( int num)

{

if (num == 0)

return 0;

else

return (testfunc2(num-1));

}

int testfunc2(int num2)

{ return testfunc1( num2-1); }

__Tail / Bottom Recursion:__

A function is said to be tail recursive, if no operations are pending when the recursive function returns to its caller.

- Such functions, immediately return the return value from the calling function.
- It is an efficient method as compared to others, as the stack space required is less and even compute overhead will get reduced.
- Recollect the previously discussed example, factorial of a number. We had written it in non tail recursive way, as after call operation is still pending.
- int fact (int n)

{

if (n==1)

return 1;

else

return (n*fact(n-1));

}

In order to make it tail recursive, information about pending tasks has to be tracked.

*Tail recursion:*

- int fact (int n)

{return (n*fact2(n-1));}

int fact2(int n, int result)

{

if (n==1)

return result;

return fact2(n-1, n*result);}

If you observe, the ‘fact2’ has similar syntax to the original fact. Now, ‘fact’ in tail recursion case does not have pending calculations / operations to perform on return from recursive function calls.

The value computed by fact2 is simply returned. Thus, amount of space required on stack reduces considerably . ( just for value of n and result , space required.)

__Linear and Tree Recursion:__

Depending on the structure the recursive function calls take, or grows it can be either linear or non linear.

It is * linearly recursive* when, the pending operations do not involve another recursive call to the function. Our Factorial recursive function is linearly recursive as it only involves multiplying the returned values and no further calls to function.

*recursion is when, pending operations involve another recursive call to function.*

*Tree*For example – Fibonacci series ; the pending operations have recursive call to the fib() recursive function to compute the results.

——————————————————————————————————-

**When not to use recursion ?**Essentially, any problem that can be solved by recursion can be also solved by iteration. Although for certain applications like Tower of Hanoi, certain artificial intelligence problems etc recursion is used since it defines the problem naturally, still there are certain times when recursion should not be used.

- Iterations are faster than their recursive counterparts. So, for speed we would normally use iteration.
- If the stack limit is constraining then we will prefer iteration over recursion.
- Some problems are naturally programmed recursively, here recursion would suit better.
- The overhead involved in recursion in terms of time and space consumed both discourages to use recursion often.