Recursion
Recursion
int main()
{
rec();
...
return 0;
}
void rec()
{
statement
1;
...
rec();
}
In the beginning main() function called rec(), then inside rec() function, it called itself again. As
you can guess this process will keep repeating indefinitely. So, in a recursive function, there
must be a terminating condition to stop the recursion. This condition is known as the base
condition.
int main()
{
callme();
}
void callme()
{
if(base_condition)
{
// terminating
condition
}
statement 1;
...
callme();
}
Often recursion can be used where loops can be used. Generally, recursive solutions are elegant
but less efficient than loop solutions. So why use recursion at all? because some algorithms can
be implemented more clearly and easily using recursion like quicksort.
1. Winding phase.
2. Unwinding phase.
Winding phase: In Winding phase, the recursive function keeps calling itself. This phase ends
when the base condition is reached.
Unwinding phase: When the base condition is reached, unwinding phase starts and control
returns back to the original call.
Let's take an example:
Example 1:
#include<stdio.h>
void rec();
int main()
{
rec(1);
void rec(int n)
{
printf("Winding phase: Level = %d\n", n);
if(n<3)
{
rec(n+1);
}
Advantages of Recursion:
Recursion provides a clean and simple way to write code.
Some problems are inherently recursive like tree traversals, Tower of Hanoi, etc. For problems,
it is preferred to write recursive code.
Using recursion, the length of the program can be reduced.
Disadvantages of Recursion:
Recursive programs are generally slower than non-recursive programs because it needs to
function call, so the program must save all its current state and retrieve them again later,
consumes more time making recursive programs slower.
Recursive programs require more memory to hold intermediate states in a stack. Non-programs
don’t have any intermediate states; hence they don’t require any extra memory.
If the programmer forgets to specify the exit condition in the recursive function, the program
execute out of memory.
Types of Recursion
There are many ways to categorize a recursive function. Listed below are some of
the most common.
Linear recursion
A linear recursive function is a function that only makes a single call to itself each time the
function runs (as opposed to one that would call itself multiple times during its execution). The
factorial function is a good example of linear recursion.
OR
It is the most commonly used recursion, where a function calls itself in simple manner and a
terminating condition is used to terminate the recursion.
long factorial(int n)
{
int f;
if(n==1) /* terminating condition */
return 1;
f=n*factorial(n-1); /* calling function itself */
return f;
}
Tail recursion
Tail recursion is a form of linear recursion. In tail recursion, the recursive call is the last thing
the function does. A good example of a tail recursive function is a function to compute the GCD,
or Greatest Common Denominator, of two numbers:
Binary Recursion
As name suggests, in binary recursion a function makes two recursive calls to itself when
invoked, it uses binary recursion. Fibonacci series is a very nice example to demonstrate binary
recursion. See the example below:
fib (1) = fib (2) = 1
fib (n) = fib (n-1) + fib (n-2), if n > 2
Multiple Recursion
Multiple recursion can be treated a generalized form of binary recursion. When a function
makes multiple recursive calls possibly more than two, it is called multiple recursion.
Mutual recursion:
Calling two or more functions mutual is called mutual recursion. Say for example, if function A is
calling B and function B is calling A recursively then it is said that, they are in mutual recursion.
Nested recursion:
When a recursive method has a parameter defined in terms of itself then it is called nested
recursion
#include<stdio.h>
int main()
{
int a,b,c,d,s;
printf("Enter four integers:\n");
scanf("%d%d%d%d",&a,&b,&c,&d);
s=sum(sum(sum(sum(a,b),c),d));
printf("Sum of four numbers %d",s);
return 0;
}
int sum(int x,int y)
{
return x+y;
}