Function references

Function can be referenced and invoked dynamically in a CALL instruction, or in an expression.

Purpose of function references

A function reference points to a function definition, that can be called at runtime. The actual function is not known at compile time, only the function type (number and type of parameters and return values) is known.

This feature allows you to manipulate functions dynamically, for example to implement generic module, that can be configured with callback functions.

Function references are based on function types. Referenced functions must be defined with the syntax defining parameter types in parentheses (and the RETURNS clause, if the functions return values), For more details, see FUNCTION syntax 2.

Function types

A function type identifies the number and types of parameters and return values of a function:

FUNCTION(p1 INT, p2 INT) RETURNS INT

A function type can be used as other types, to declare simple variables, members of a structured RECORD, or arrays.

User-type definition for function references

To simplify function reference usage, you can define a user-type with the TYPE instruction, using the function type that will match functions to be called by reference:

TYPE callback_function FUNCTION(p1 INT, p2 INT) RETURNS INT

This type can be used as other types, to declare simple variables, members of a structured RECORD, or arrays.

For more details about user-defined type definitions, see Types.

Variable definition for function references

When the user-type for the function reference is available, declare a program variable to hold such function reference:

DEFINE callback callback_function

For more details about variable definitions, see Variables.

Get the FUNCTION reference

To get the reference of a function, use the FUNCTION keyword followed by the name of the function to be referenced. The function must be defined in the current module, or in a module imported with IMPORT FGL.

LET callback = FUNCTION add

In the above example, the function "add()" must be defined with the same function type as the "callback" variable.

For more details, see FUNCTION func-spec.

Invoking a function with the CALL statement

Functions referenced in a variable can be invoked with the CALL instruction, by using the variable. The referenced function will be called as in a regular function call:
CALL callback(100,200) RETURNING result

Using function references in expressions

Variables referencing functions can be used in expressions, like in this example:
LET get_count_func = FUNCTION get_total_items()
LET c = get_count_func()
LET get_count_func = FUNCTION get_total_elements()
LET c = c + get_count_func()

Passing function references as function parameters

Like other values, function references can be passed as function parameters:
CALL process( FUNCTION add, FUNCTION sub, callback )
...
FUNCTION process( f1 callback_function,
                  f2 callback_function,
                  f3 callback_function )
   DISPLAY f1(100,200) + f2(200,50) + f3(150,300)
END FUNCTION