# Thread: Explanation of functions needed.

1. ## Explanation of functions needed.

The following is stated in the book I am reading at the moment.
Code:
```int factorial(int x)
{
int i;
for(i=1; i < x; i++)
x *= i;
return x;
}```
Code:
```int a=5, b;
b = factorial(a);```
Gives variable b the value 120 at the end. Could someone explain to me how this is calculated?

2. It doesn't. The code is wrong.

3. Originally Posted by King Mir
It doesn't. The code is wrong.
Am I right when I say the result is 25 or what exactly is wrong. Ow, yeah, maybe handy to say but it is pseudo code if I am right.

4. It's trying to modify x, which is an input. Pretty sure while that input x is local to main(), the x referenced inside of factorial() is a different variable (but of the same name), local to factorial().

The way it's supposed to work though is you get an input, say 7, and multiply 7 by every integer between 1 and 7...that code would take the x (7) and multiply it by 1, store it to x...take x, multiply by 2, store, take x, multiply by 3, store, up until 6 (7 < 7 breaks the loop), effectively giving you 7 * 6 * 5 * 4 * 3 * 2 * 1.

5. Originally Posted by Epy
It's trying to modify x, which is an input. Pretty sure while that input x is local to main(), the x referenced inside of factorial() is a different variable (but of the same name), local to factorial().

The way it's supposed to work though is you get an input, say 7, and multiply 7 by every integer between 1 and 7...that code would take the x (7) and multiply it by 1, store it to x...take x, multiply by 2, store, take x, multiply by 3, store, up until 6 (7 < 7 breaks the loop), effectively giving you 7 * 6 * 5 * 4 * 3 * 2 * 1.
I'm an idiot. Now I see it.

Thanks for the help the both of you.