UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
Recursive Function
1. Prepared by-
Harsh Pathak
Guided by – Prof. Rohit Singh
Gandhinagar Institute of Technology
SUBJECT-CPU (2110003)
Recursive Function
2. Contents
1 What is Recursive Function?
2 Advantage and Disadvantage
3 Program
4 Output
Recursive Function
2
3. What is Recursive Function?
When a called function in turns calls another
function a process of ‘chaining’ occurs. Recursion
is a simple special case of this process, where a
function call itself.
Recursive Function
3
4. A very simple example of recursion is presented
below:
main( )
{
printf(“This is an example of recursion n”);
main( );
}
When executed this program will produce an output
:
This is an example of recursion
This is an example of recursion
This is an example of recursion
The Execution will continue indefinitely.
Recursive Function
4
5. Advantage and Disadvantage
• Advantages
*Easy solution for recursively defined problems.
*Complex programs can be easily written in less
code.
• Disadvantage
*Recursive code is difficult to understand and
debug
*Terminating condition is must, otherwise it will go
in infinite loop.
*Execution speed decreases because of function
call and return activity many times.Recursive Function
5
6. PROGRAM
#include<stidio.h>
#include <conio.h>
long int fact (int n); /*prototype of function*/
int main()
{ int m;
long int ans; /*long in store large number*/
clrscr();
printf("Enter the number:n);
scanf("%d", &m);
ans=fact(m); /*call fuction fact() with call by value*/
printf("Factorial of %d using function = %ld", m, ans);
}
long int fact(int n) /*function body here*/
{ if (n == 1|| n==0)
return 1;
else
return n*fact(n-1); /*recursion here. fact() calls fact()*/
}
6
7. OUTPUT
Give the number
6
Factorial of using function=720
Give the number
0
Factorial of using function=0
Cox / RixnerArrays and Pointers7