FUNCTION definitions
A FUNCTION
definition defines a named procedure with a set of
statements.
Syntax 1 (legacy syntax):
[PUBLIC|PRIVATE] FUNCTION function-name ( parameter-name [,...] )
[ parameter-definition [...] ]
[ local-declaration [...] ]
[ instruction
| [ RETURN expression [,...] ]
[...]
]
END FUNCTION
- function-name is the function identifier.
- parameter-name is the name of a formal argument of the function.
- parameter-definition is a
DEFINE
instruction for a parameter. - local-declaration is a
DEFINE
,CONSTANT
orTYPE
instruction. - instruction is a language statement to be executed when the function is invoked.
- expression is a value to be returned by the function.
Syntax 2 (fully typed):
[PUBLIC|PRIVATE] FUNCTION function-name (
parameter-name data-type
[ ATTRIBUTES ( attribute [ = "value" ] [,...] ) ]
[,...]
)
[ RETURNS { data-type | ( data-type [,...] ] ) } ]
[ local-declaration [...] ]
[ instruction
| [ RETURN expression [,...] ]
[...]
]
END FUNCTION
- function-name is the function identifier.
- parameter-name is the name of a formal argument of the function.
- data-type can be a built-in data type, a user defined type, a built-in class, an imported package class, or a Java class.
- attribute is an attribute to extend the variable definition with properties.
- value is the value for the variable attribute, it is optional for boolean attributes.
- local-declaration is a
DEFINE
,CONSTANT
orTYPE
instruction. - instruction is a language statement to be executed when the function is invoked.
- expression is a value to be returned by the function.
Example of function definition using legacy syntax
The following example shows a function definition using the legacy syntax, with parameter
definition in the function body:
FUNCTION split(str, len)
DEFINE str STRING, len INT
RETURN str.subString(1, len),
str.subString(len+1, str.getLength())
END FUNCTION
This syntax does not define a function with a complete signature.
Example of function definition with complete function type
By specifying data types in the parameter list, you define a function with a complete function type.
This syntax allows better compilation
checking:
FUNCTION split(str STRING, len INT) RETURNS (STRING, STRING)
RETURN str.subString(1, len),
str.subString(len+1, str.getLength())
END FUNCTION
Braces after the
RETURNS
clause are not required, when the function returns a single
value:FUNCTION count_items(sid INT) RETURNS INT
DEFINE cnt INT
SELECT COUNT(*) INTO cnt FROM stock WHERE stock_id = sid
RETURN cnt
END FUNCTION
The next example defines a function, that does not return
values:
FUNCTION append(name STRING)
DISPLAY "Do something with name..."
END FUNCTION
Example of function without parameters and without return values
In this example, the function has no parameters and does not return values:
FUNCTION clean_debug_log()
CALL os.Path.delete("log.txt")
END FUNCTION