Statement: function
A function statement defines a function that can be used in an expression with the function operator '@'. The dot operator can be used to add an optional qualifying string to the function name. Arguments may also be passed to the function. These, with the qualifier, become local variables within the function. When calling the function, these values may be given values or omitted. An argument can be given a default value when the function is defined. The default for the qualifier is an empty string.
When the function is run, the arguments are treated as local variables. An addition local variable named 'result' is created, and this is the value given to the function when it used in an expression. Additional variables that are created within the function will be local and their values lost when the function completes. Variables created outside of the function cannot be seen whilst inside.
The statement 'end;' can be used at any time to exit the function immediately. This will just exit the function, not the script.
function Statement Format |
---|
function name.qualifier(arg[=value], arg[=value]) { ... result = expr; ... } write @name.qualifier(value1, value2); write @name(value1); write @name.qualifier; write @name; |
function name { ... result = expr; ... } write @name; |
function Statement | |
---|---|
Example | Output |
function range_plus(start, duration = 7){ if duration < 1 result = @error( "Duration must be above zero." ); end; endif result = start .. start + duration - 1; } let r = @range_plus(100); write r + ", Duration: " + r[.span] nl; write @range_plus( 100, 0 ); | 100..106, Duration: 7 Error (3): Duration must be above zero. |
// Requires the hics extensions. function this_week.day_start { if day_start = "sun" let day = @record.jwsn( today )[day]; else let day = @record.jwn( today )[day]; endif let start = today - day + 1; result = start .. ( start + 6 ); } write @text( @this_week ) nl; write @text( @this_week.sun ); | 10 Apr 2023..16 Apr 2023 9 Apr 2023..15 Apr 2023 |
Written when today = "g:dmy# 14 Apr 2023" and uses the hidden schemes "jwn" and "jwsn". |