Problem with variables inside a function

Problem with variables inside a function

Return to topic list

Thu Aug 28 20:33:29 2014   by   Daniel
Hi again Torben,

Im running Troll on my Linux desktop.

When I put a variable inside a function, it doesn't works, see below:

simulacro.t
--------------------------------------------
function reroll(m) =
roll:= sum numdices d faces;
if roll > 9 U roll < -9 then call reroll(0)
else roll
call reroll(0)
--------------------------------------------

When I try on Troll...

$./troll simulacro.t 0 numdices=3 faces=8
Distribution error: unknown variable: faces at line 2, column 23

--------------------------------------------
It does'n reconize the variables...
 
Fri Aug 29 09:20:15 2014   by   Torben
Functions in Troll can only use variables that are defined inside the function (or as parameters).  There is no concept of global variables: The variables that are defined on the command line are "local" to the expression that is defined outside functions.  Conceptually, this expression is inside a nameless function definition. So if you want to use numdices and faces inside reroll, you should pass these as parameters (including in the recursive call).
 
Fri Aug 29 21:31:52 2014   by   Daniel
Thanks Torben, it works
 

Return to topic list



New message:
Topic:
Posted by:

Type the values of the dice shown below:

Return to topic list