In C programming language when a function calls itself over and over again , that function is called recursive function .The process of function calling itself repeatedly is known as recursion .
Recursion is supported by C programming language i.e., a function to call itself repeatedly again and again and while using this function the programmer needs to be careful that there should be an exit function in the program,else it will go in infinite loop.
In the C recursive function it allow us to break a complex problem into identical sub-problems recursively until each and every sub problem are simple enough so that they can be solved directly in C language.Then at last the solutions of the identical sub problems are combined to make the final solution.
Advantage of Recursion
In recursion we use the divide and conquer approach to divide the original problem to sub problem and hence replaces complex nesting code.
Disadvantage of Recursion
C Recursion uses lot of stack space, usually it is not used when the program is small and running on a PC. It is hard to debug the code or extend the functionality in case of recursive logic.
Below is the code example of Factorial using recursion
#include <stdio.h>
#include <conio.h>
int facto (int);
int main()
{
int numb,p;
printf(\nEnter a positive number:);
scanf(%d,&numb);
return 0;
}
int facto(int m)
{
if(n==0)
return 1;
else
return(n*facto(m-1));
}
Output:
Enter a positive number: 5
Factorialof 5 is:120
0 Comment(s)