HyperMath

Multiple Returns

Multiple Returns

Previous topic Next topic No expanding text in this topic  

Multiple Returns

Previous topic Next topic JavaScript is required for expanding text JavaScript is required for the print function  

HyperMath can return more than one value from a function.  This is done by returning a comma separated list of values (the function returns at that point, too):

> function foo(angle) return Cos(angle), Sin(angle); end

> print( foo(1) )     // returns 2 values...

0.54030230586814        0.8414709848079

>

> c,s = foo(3.142/3)  // assign the values to variables

>

> print(c,s)

0.49988240461137        0.86609328686923

It is possible to assign only the first few outputs to variables.  The rest of the returns are ignored.

> c = foo(3.142/3)

> print(c)

0.49988240461137

If extra variables are assigned on the left hand side, those are set to nil.

> c,s,z = foo(3.142/3)

> print(c,s,z)

0.49988240461137        0.86609328686923        nil

Note

When data of type table is returned, a reference to it is returned instead of a copy.  To return a copy, use CopyTable() function. This matters when returning global variables.