This code gives output as 125
#include<stdio.h>
int func(int a)
{
static int num = 2;
if(a==0) return 1;
num++;
return num*func(--a);
}
int main()
{
printf("%d", func(3));
}
While this code gives output as 60
#include<stdio.h>
int func(int a)
{
static int num = 2;
if(a==0) return 1;
return (++num)*func(--a);
}
int main()
{
printf("%d", func(3));
}
In the first code I incremented num before return statement, in second code I pre-incremented num in the return statement.
The first code seems to be evaluating to 5*5*5, while the second evaluates to 3*4*5. Why is it so?