# don't understand this code

Hi I was reading through howstuffworks for c. Can somebody explain how this is a factorial code

``````int fact(int i)
{
int j,k;

j=1;
for (k=2; k<=i; k++)
j=j*k;
return j;
}
``````

so for i = 5 we would go through this loop until k <=5. can't figure out the j=j*k expressions. when we say return j does that mean we start at j=1 and go through the loop and then go to the j=2, go through the loop, j=3 etc? (if so where is the j++)

not sure what the return j signifies here since j=j*k

pretty confused overall how this works =/

## 1 Answer

can't figure out the j=j*k expressions

The expression `j = j * k` means multiply the value that is currently stored in `j` by the value stored in `k` and store the result back in `j`.

when we say return j does that mean we start at j=1 and go through the loop and then go to the j=2, go through the loop, j=3 etc?

Not really. `return j` is what's known as a return statement. The function named `fact` has a return type of `int` and this basically means that it returns an `int` value to its caller. For more information, you may watch the short on functions!

Now,there is also another trick here and that is when the body of a for loop is not delimited by curly braces (i.e. `{` and `}`), the first statement right after the loop is considered the body of that loop and it is the one whose execution repeats as long as the loop continuation condition, in this case the `k <= i`, is evaluated to true.

Any subsequent statement is outside of the body of the loop. So the statement `return j` is actually executed after the loop ends execution.

where is the j++

The variable `j` is used to hold the factorial of the input `i`. `j++` is capable of increasing the value in `j` by one. Why would you ever need to do that?

• ah i see your explanation of j=j*k was the key. thanks! – rp310 Feb 7 '15 at 18:35
• @rp310 you may up-vote the answer and accept it if it answers your question! – kzidane Feb 7 '15 at 20:43