No. Factorial( a - 1 ).
Factorial( 10 );
if it is > 1 it will multiply a by a - 1. If a == 1 then it multiplies a by 1. So there yeah line 8 is 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 but line 10 [b]is[b] being called. because with line 8 you are calling factorial( a - 1 ) so when a is 2. that would mean it returns 1 because that is less than 2.
Basically say you have 10. It is like doing
1 2
|
short total( 1 ) , start( 10 );
while( start > 2 ){ --start; total *= start; }; // you can do > 1 but anything times 1 is that number...
|
Recursion is calling the function multiple times.
You are calling the function while the value is greater than 1 and decreasing the value each time and if the value is equal to 1 then you end the recursion and do not call the function again. if you put
1 2 3 4
|
long long factorial( long a )
{
return( a * factorial( a - 1 );
}
|
It will loop through the function for ever and anyways it would just be 0 but would take eternity to get that answer which actually you never even get because the program would just crash
**edit**actually I don't think the program would crash because after it reaches the negative limit it would turn back into a random positive integer and anything times 0 is always zero therefor would just run forever.
**EDIT2**
Basically if the current value of a is greater than 1 it will keep calling the factorial function with a - 1 as a parameter and if a == 1 then it will not call the factorial function anymore therefore returnning the correct result. The reason you are thinking it does nothing is because it returns 1 and not a but that doesn't matter it is returning 1 to the factorial function being called before it not the very first factorial function that is returning the actual value.
It is returning the value to the call before it.
it would be something like
a = a * b * c * d * e * f * g * h * i * j;
b = a - 1;
c = b - 1;
d = c - 1;
e = d - 1;
f = e - 1;
g = f - 1;
h = g - 1;
i = h - 1;
j = 1;
so a = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
http://www.cprogramming.com/tutorial/lesson16.html
http://www.cplusplus.com/doc/tutorial/functions2/
http://www.danzig.us/cpp/recursion.html