FUNCTION function-name(parameters) statement-block ENDFUNCTION |
0 or more parameters can be defined. |
A statement-block is zero or more statements. Variables can be declared here, and will go out of scope when the statement-block is exited.
Note, this construct uses fully bracketed syntax.
Parameters can have default values specified, by adding ":= [default_value]". See below for examples. Default parameter values must be single scalar values - an integer, a real, or a string.
Parameters may also be passed by name. Once a parameter has been passed by name, remaining parameters must all be passed by name, or have default values and not be specified.
Examples
function foo() print( "foo" ); endfunction foo(); |
foo |
function foo() print( "foo" ); print( "bar" ); endfunction foo(); |
foo bar |
function foo( a ) print( "hello " + a ); print( "bar" ); endfunction foo( 5 ) foo( "world" ); |
hello 5 bar hello world bar |
function foo( a := "world" ) print( "hello " + a ); endfunction foo( 5 ); foo(); |
hello 5 hello world |
function foo( a := "hello", b := "world" ) print( a + " " + b ); endfunction foo(); foo( b := "sailor"); foo( "so long" ); foo( a := "so long" ); foo( "goodbye", b := "all" ); |
hello world hello sailor so long world so long world goodbye all |
function foo( a := "hello", b := "world" ) print( a + " " + b ); endfunction foo( a := "stuff" , "it"); |
An error! Since a was passed by name, b must be passed by name or by default. |
function foo( a := 6 / 3, b := "blah" ) print( a + " " + b ); endfunction foo() |
2 blah |