Attention

IronPLC can only run very simple programs. The steps described are accurate but many language features are not yet supported.

Function Call

Functions and function blocks are invoked using call syntax that passes arguments and receives results.

IEC 61131-3

Section 3.3.2.2

Support

Partial

Syntax

Function call (expression)

result := function_name ( argument_list ) ;

Function block instance call (statement)

instance_name ( input_assignments ) ;
output_value := instance_name.output_name ;

Description

Functions are called within expressions and return a value. Function blocks are called as statements on a previously declared instance variable; outputs are accessed by qualifying the instance name with the output name.

Positional arguments pass values in the order of the input parameter declarations:

result := MyFunc(10, 20);

Named (formal) arguments explicitly associate values with parameter names using the := notation:

result := MyFunc(x := 10, y := 20);

Positional and named arguments must not be mixed in a single call.

Function block calls use named arguments for inputs. After the call, outputs are read from the instance:

my_timer(IN := start_signal, PT := T#5s);
elapsed := my_timer.ET;
done := my_timer.Q;

Example

FUNCTION Add : DINT
    VAR_INPUT
        a : DINT;
        b : DINT;
    END_VAR

    Add := a + b;
END_FUNCTION

PROGRAM main
    VAR
        result : DINT;
    END_VAR

    (* Positional call *)
    result := Add(3, 4);

    (* Named call *)
    result := Add(a := 10, b := 20);
END_PROGRAM

See Also