Attention
IronPLC can only run very simple programs. The steps described are accurate but many language features are not yet supported.
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 parameters and local VAR variables.
Functions must not have VAR_OUTPUT or VAR_IN_OUT parameters
(use function blocks for those).
Calling a Function¶
Functions can be called using positional or named (formal) arguments:
(* Positional *)
result := Square(42);
(* Named *)
result := Square(x := 42);
See Also¶
FUNCTION_BLOCK — stateful callable unit
PROGRAM — top-level executable unit
Function Call — call syntax