Recursive Function in C Language

This “Recursive Function in C” page explains clearly how to define recursive function in programming and execution flow with diagrammatic representation.


 

Recursion :

Function calling itself
(or)
Calling a function from the same function definition.

#include<stdio.h>
void main(void)
{
printf(“main\n”);
main();
printf(“end\n”);
}

  • In the above program, main() function execution never ends.
  • Program terminates abnormally when stack memory is full.

 

 

WAP to find factorial of a given number using recursion.
#include<stdio.h>
int fact(int);
void main(void)
{
int n,factorial;
printf(“Enter one number : “);
scanf(“%d”,&n);
factorial = fact(n);
printf(“result : %d\n”,factorial);
}
int fact(int n)
{
int factorial;
if(n==0)
{
factorial=1;
}
else
{
factorial = n*fact(n-1);
}
return factorial;
}

Share this