I'm learning about functions in C from the book C Programming: A Modern Approach. It contains the following function:
int fact(int n)
{
if (n <= 1)
return 1;
else
return n * fact(n - 1);
}
with the following explanation:
To see how recursions works, let's trace the execution of the statement
i = fact(3);
here is what happens: "fact(3) finds that 3 is not equal or less than 1, so it calls fact(2)which finds that 2 is not equal or less than 1, so it calls fact(1) which finds that 1 is equal or less than 1, so it returns 1,causing fact(2) to return 2 x 1 = 2, causing fact(3) to return 3 x 2 = 6
I'm really confused, why does it does all these actions if it is not inside a loop? why does it keep making the 3 less and less until it matches the argument in if function?
Does the * symbol means that n in the fact(n) is now 3 - 1 and it basically says it do it all over again?