Recursive Function

HarshPathak28 9,078 views 8 slides Apr 08, 2017
Slide 1
Slide 1 of 8
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8

About This Presentation

This presentation covers the important part of C language.Example is also included which helps you to understand easily


Slide Content

Prepared by- Harsh Pathak Guided by – Prof. Rohit Singh Gandhinagar Institute of Technology SUBJECT-CPU ( 2110003) Recursive Function

Contents 1 What is Recursive Function ? 2 Advantage and Disadvantage 3 Program 4 Output Recursive Function 2

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

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

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

PROGRAM #include< stidio.h > # include < conio.h > long int fact ( int n ); /*prototype of function*/ int main() { int m; l ong int ans ; /*long in store large number*/ c lrscr (); 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; e lse return n*fact(n-1); /*recursion here. fact() calls fact()*/ } 6

OUTPUT Give the number 6 Factorial of using function=720 Give the number Factorial of using function=0 Cox / Rixner Arrays and Pointers 7

8