In computer often, we have to use a recursive function. Often it is easy to perform a routine by using a recursive function than using a loop.
Recursive Function: is one that calls itself. A main function cannot be use as a recursive function. Must have a base function from which we call the recursive function and after at one point control will back to the same function.
Not all programming languages support recursion. Want to use Iteration (Loops) or Recursion it is dilemma, please follow these and take you decision :
- Iteration is faster and occupies less memory.
- In some problems will be easier using recursion.
- All Recursive function may always be replace by iteration.
- Converting Recursive function to iteration is called “unrolling”.
Advantage of Recursion
- Expressive Power
- Shorter code in recursion than iterative.
- Problems it is more appropriate.
Disadvantage of Recursion
- Slower than Iteration
- Big programs will be very difficult to debug.
- Use of more memory space
I will give a few Programs that use recursive function. Remember that every recursive function needs a logical escape route, otherwise system will hang.
In first program, I am giving you a factorial of ‘N’ number. I am using Integer declaration; it will work up to a smaller number only for big value use long or double. Also, check for negative values.
//Factrial using Recursion - C
#include <stdio.h>
#include <conio.h>
//sending n-1 and when value of n=1 the resul return to main function
static int fact(int n) //Recursive function
{
if(n<=1)
{
return n;// return to main program
}
else
{
return n*fact(n-1);// calling self
}
}
void main()
{
int n,f;
clrscr();
printf("Enter a No. : ");
scanf("%d",&n);
f=fact(n);
printf("Factorial of %d = %d ",n,f);
getch();
}
//Fibonacci Series - C
#include <stdio.h>
#include <conio.h>
int fibo(int n,int a,int b)
{
int c;
if(n<=1)
{
return 0;
}
else
{
c=a+b;
printf(" %d ",c);
return fibo(n-1,b,c);
}
}
//this main function we are not printing anything
void main()
{
int n;
clrscr();
printf("Enter a No. : ");
scanf("%d",&n);
n=fibo(n,1,0); // 1 and 0 the value of a and b
getch();
}
//Factrial using Recursion - Java
import java.io.*;
public class Rfactorial
{
//sending n-1 and when value of n=1 the resul return to main function
static int Rfact(int n) //Recursive function
{
if(n<=1)
{
return n; // return to main program
}
else
{
return n*Rfact(n-1); // calling self
}
}
public static void main(String args[])throws IOException
{
int n,f;
BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a No. : ");
n=Integer.parseInt(Br.readLine());
f=Rfact(n);
System.out.println("Factorial of "+n+" = "+f);
}
}
//Fibonacci Series - Java
import java.io.*;
public class Rfibo
{
static int fibo(int n,int a,int b)
{
int c;
if(n<=1)
{
return 0;
}
else
{
c=a+b;
System.out.print(c+" ");
return fibo(n-1,b,c);
}
}
//here in main function we are not printing anything
public static void main(String args[])throws IOException
{
int n;
BufferedReader Br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a No. : ");
n=Integer.parseInt(Br.readLine());
n=fibo(n,1,0); // 1 and 0 the value of a and b
}
}
!!!Flying to Fiji Island and say you Moce Mada but in reply you have to say Vinaka!!!