Why do we use return 1 to terminate the recursive function? Can any other value be used as a default value like 1.
And if we return 1 as return value of the function, then why 1 is not returned to main function.
#include<stdio.h>
int fact(int n)
{
if(n>=1)
return (n*fact(n-1));
else
return 1;
}
int main()
{
int a,ans;
scanf("%d",&a);
ans=fact(a);
printf("factorial of %d is %d ",a,ans);
return 0;
}
/*
explanation
fact(4);
if(4>=1) 4*fact(3)
if(3>=1) 4*3*fact(2)
if(2>=1) 4*3*2*fact(1)
if(1>=1) 4*3*2*1*fact(0)
if(0>=1) return 1;
*/
fact(4), notfact(0)fact(0)and use a debugger to see that1is indeed returned tomain