Calling functions using named arguments

To pass argument values to a function, an alternative way is to use a pair of braces, and ``name'' the arguments. For example, instead of f(3,9), one can use
f{x=3, y=9}
Here x and y must be the same formal argument names declared in the definition of the function.

Calling functions this way has two advantages. Firstly, the order of the arguments doesn't matter. For example f{x=3, y=9} and f{y=9, x=3} would be the same. Secondly, you don't have to pass a value for every argument. Any arguments not named in the braces will take default values (specified in the function definition).

Note that an expression {x=3, y=9} is exactly the same as the syntax for defining a structure with fields x=3, y=9. So passing arguments as named parameters is similar to passing a structure to the function.



oz 2009-12-22