Understanding functions

This is an introduction to functions.

Function basics

Functions are named program blocks containing a set of statements to be executed when the function is invoked with a CALL statement, or when the function is used in an expression, or when the function is registered in a callback mechanism like WHENEVER ERROR CALL.

Parameters and return values

Functions can get input parameters and return zero, one or several values.

Scope of functions

A function is defined in a program module, and is by default visible to all modules (it is PUBLIC). A function can be declared as PRIVATE to the module where it is defined. In this case the function is hidden to other modules. This concept is explained in Scope of a function

Function references

It is possible to hold a function reference in a program variable. The variable can then be assigned with any function reference that is declared with the same function signature as the type used to define the variable.

Note: Function references apply only to regular functions. It is not possible to use a method reference.

Functions as methods for a type

When declaring a function with a receiver variable and receiver type enclosed in parentheses before the function name, it becomes a method acting on the specified type.

Use methods in conjunction with interfaces to write clear robust code by using encapsulation and polymorphism concepts.

Function attributes

Functions can be declared with function attributes, to define additional information on the function.