Help me on this factorial formula bug

0 favourites
• 10 posts
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
• HI, I'm trying to create a function do display the result of a factorial calculation.

The problem is, it only works once.

This is the result, when i entered '5' the second time, the result is different

• In line 5 you need to "set result to 1". This will fix it.

However, I still don't understand, why does it work like this. Variables are local and not static, so they should reset after each attempt. Can someone explain?

• dop2000 my guess: it's a recursion in line 6. The function calls itself and the values are stored on the stack. If the line 6 isn't true anymore the stack will be processed.

• yes it's a recursive function.

• dop2000 thanks it's working now

• Asmodean Yes, it's recursive, but it uses local variables for calculations, which are reset to default values on every tick. So if you press the button multiple times to call the function for the same number (5), every time it should return the same result. I don't understand why the results are different.

• Do you know what's the difference between setting "result to 1" and "set return value to 1" ?

• Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• "result" is the name of your variable.

• dop2000 The whole function make no sense to me. It shouldn't work, because result= f(val-1)*val would only calculate val-1*val each time. If you use result=0 you get 0 as return value. If you made the variable global it doesn't work either.

The function should look like:

• Asmodean No, apart from that little mistake in line 5 the function does work. It's just not as optimized as your version.

• 10 posts