FUNCTION¶
A function is a stateless callable unit that returns a single value. Functions do not retain state between calls.
IEC 61131-3 |
Section 2.5.1 |
Support |
Partial |
Syntax¶
FUNCTION function_name : return_type
variable_declarations
statement_list
END_FUNCTION
The function returns a value by assigning to the function name.
Example¶
FUNCTION Square : DINT
VAR_INPUT
x : DINT;
END_VAR
Square := x * x;
END_FUNCTION
Functions may have VAR_INPUT, VAR_IN_OUT and VAR_OUTPUT
parameters and local VAR variables. Because a function has no state, it
cannot declare or invoke a function block instance; that is reported as
P4054.
Output Parameters¶
A VAR_OUTPUT parameter gives the caller a value besides the function’s
result. A call stores it in a variable with => after the function
returns:
FUNCTION DivMod : DINT
VAR_INPUT
a : DINT;
b : DINT;
END_VAR
VAR_OUTPUT
remainder : DINT;
END_VAR
DivMod := a / b;
remainder := a MOD b;
END_FUNCTION
quotient := DivMod(a := 7, b := 2, remainder => rest);
An output starts from its initial value on every call, as a local variable
does. It is converted to the type of the variable it is stored in, as an
assigned value is, and one that variable cannot hold is
P4035. NOT done => idle
stores the inverse of a BOOL output.
In/Out Parameters¶
A VAR_IN_OUT parameter is passed by reference: the function reads and
writes the caller’s variable, so a change inside the function is visible to
the caller after the call returns.
FUNCTION AddTo : DINT
VAR_INPUT
amount : DINT;
END_VAR
VAR_IN_OUT
total : DINT;
END_VAR
total := total + amount;
AddTo := total;
END_FUNCTION
PROGRAM main
VAR
sum : DINT := 100;
result : DINT;
END_VAR
result := AddTo(amount := 42, total := sum); (* sum is now 142 *)
END_PROGRAM
The argument for a VAR_IN_OUT parameter must be a variable
(P4058) of exactly the
parameter’s type (P4059) that
the compiler can prove is writable: not CONSTANT, and not a VAR_INPUT
of the calling POU (P4060).
Calling a Function¶
Functions can be called using positional or named (formal) arguments:
(* Positional *)
result := Square(42);
(* Named *)
result := Square(x := 42);
Positional arguments go to the VAR_INPUT and VAR_IN_OUT parameters in
the order the function declares them. A call supplies every one of them
(P4018), even one that declares an
initial value: a function keeps nothing between calls. Named arguments may be
written in any order, each parameter at most once
(P4024), and the call evaluates
them in the order the function declares its parameters.
See Also¶
FUNCTION_BLOCK — stateful callable unit
PROGRAM — top-level executable unit
Function Call — call syntax