Subroutines - Return Statements
DEFINE_FUNCTION also allows the use of the RETURN keyword that serves two purposes:
- To return prematurely from a function.
- To return a value from a function.
The format of the return statement is:
If a return statement is encountered anywhere in the function, execution of the function is
terminated immediately and the value (if any) specified as the <return value> is returned to the
caller.
A function that returns a value through the RETURN keyword must be declared with a return type. Conversely, a function that is declared without a return type cannot return a value. The return type may only be one of the 8 intrinsic data types. Strings, arrays, structures, classes and other user-defined types may not be returned.
When using DEFINE_FUNCTION with a return type CHAR, it is necessary to specify the length of the return value. For example:
In the example below, GetBufferSize returns an unsigned 16-bit integer, BufSize. The return type
is indicated before the DEFINE_FUNCTION keyword.
To call this function and to retrieve the RETURN value, use the following syntax:
where BufSize is declared to be of type INTEGER.
Even if a function returns a value, it is not necessary to assign the return value to a variable. Both forms of the following call are valid:
In the second case, the return value is simply thrown away.