Hundreds of features to explore
Games made in Construct
Your questions answered
Popular & trusted by schools and Universities world-wide
Construct 3 runs in the browser & works offline
Students do not need accounts with us
Our educational partners
Free education resources to use in the classroom
World class complete documentation
Official and community submitted guides
Learn and share with other game developers
Upload and play games from the Construct community
Game development stories & opinions
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.
Develop games in your browser. Powerful, performant & highly capable.
Do you know what's the difference between setting "result to 1" and "set return value to 1" ?
"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.