P
Paminu
I have this code (factorial function):
int fact (int n)
{
if (n < 1)
{
return 1;
}
else
{
return (n * fact(n-1));
}
}
int main()
{
printf("result: %d\n",fact(3));
return 0;
}
When I run the program the result is: 6. But at some point n = 0 and (n<1)
becomes true and therefore "return (1)" is executed. How can the result be
6 (which is correct) when the last thing this function does is return 1?
int fact (int n)
{
if (n < 1)
{
return 1;
}
else
{
return (n * fact(n-1));
}
}
int main()
{
printf("result: %d\n",fact(3));
return 0;
}
When I run the program the result is: 6. But at some point n = 0 and (n<1)
becomes true and therefore "return (1)" is executed. How can the result be
6 (which is correct) when the last thing this function does is return 1?