Saladsamurai
- 3,009
- 7
Hi
I am relatively new to C++ and I am having a little trouble understanding, in detail, the logic
of this recursive function.
Can someone tell me if my reasoning this out is correct?
So if the user enters 1, it returns 1. But if the user enters 3... what happens here?
it returns 3*fact(2)
and then fact(2) returns 2*fact(1)
and fact (1)=1
so we have 3*2*1= 3!
okay...so I guess I do get it kind of.
But when it says "return (n*fact(n-1)" where exactly is it "returning" it?
It seems like it means "return it to the argument of the function".
Is that more or less correct? Especially what is in boldface...
I am relatively new to C++ and I am having a little trouble understanding, in detail, the logic
of this recursive function.
Can someone tell me if my reasoning this out is correct?
Code:
int fact (int n)
{
if (n==1)
return 1;
else
return (n*fact(n-1));
}
it returns 3*fact(2)
and then fact(2) returns 2*fact(1)
and fact (1)=1
so we have 3*2*1= 3!
okay...so I guess I do get it kind of.
But when it says "return (n*fact(n-1)" where exactly is it "returning" it?
It seems like it means "return it to the argument of the function".
Is that more or less correct? Especially what is in boldface...