Problem
You have some code that you want to call from more than one place, maybe with different arguments.
Solution
Use a user written function, with parameters, local variables to keep them away from other code, and a return statement to send back an answer.
rollDice = Function( {ndice = 2, nsides = 6},
{i, sum = 0},
For( i = 1, i <= ndice, i++,
sum += Random Integer( 1, nsides )
);
Return( sum );
);
dt = New Table( "Untitled",
Add Rows( 10000 ),
New Column( "one", Formula( rollDice( 1, 71 ) ) ),
New Column( "two", Formula( rollDice( 2, 35 ) ) ),
New Column( "three", Formula( rollDice( 3, 23 ) ) ),
New Column( "four", Formula( rollDice( 4, 17 ) ) )
);
dt << Distribution(
Continuous Distribution( Column( :one ) ),
Continuous Distribution( Column( :two ) ),
Continuous Distribution( Column( :three ) ),
Continuous Distribution( Column( :four ) ),
SendToReport(
Dispatch( {"one"}, "Distrib Histogram", FrameBox, {DispatchSeg( Hist Seg( 1 ), Bin Span( 24, 0 ) )} ),
Dispatch( {"two"}, "Distrib Histogram", FrameBox, {DispatchSeg( Hist Seg( 1 ), Bin Span( 24, 0 ) )} ),
Dispatch( {"three"}, "Distrib Histogram", FrameBox, {DispatchSeg( Hist Seg( 1 ), Bin Span( 24, 0 ) )} ),
Dispatch( {"four"}, "Distrib Histogram", FrameBox, {DispatchSeg( Hist Seg( 1 ), Bin Span( 24, 0 ) )} )
)
);
More dice, smaller standard deviation
Discussion
The Function() function returns a function. The first argument to Function() is a list of parameters, and is required. Use {} if you have no parameters. The parameters can have default values as shown above; rollDice() will use two 6-sided dice. rollDice(3) will use three 6-sided dice, and rollDice(1,7) uses one 7-sided die.
The second argument to function is an optional list of local variables, which can have initial values. By using local values, you can have functions call other functions and not worry about reusing "i" in a for-loop. If you don't need to specify locals, just put the function body as the 2nd argument rather than the third.
The final argument (2nd or 3rd) is the function body. It can reference global values if needed. You can use a return statement anywhere in the body to specify a value to send back to the caller. If there is no return statement, the value of the last statement executed is returned.
The function rollDice is not executed when it is first defined. It is executed when it is called with arguments, like
rollDice(3,6)
which the example does in the column formulas.
See Also
http://www.jmp.com/support/help/Advanced_Programming_Concepts.shtml