Attention

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

FUNCTION_BLOCK

A function block is a stateful callable unit with input and output parameters. Function block instances retain their state between calls.

IEC 61131-3

Section 2.5.2

Support

Partial

Syntax

FUNCTION_BLOCK fb_name
    variable_declarations
    statement_list
END_FUNCTION_BLOCK

Example

FUNCTION_BLOCK Counter
    VAR_INPUT
        reset : BOOL;
    END_VAR
    VAR_OUTPUT
        count : INT;
    END_VAR
    VAR
        internal : INT := 0;
    END_VAR

    IF reset THEN
        internal := 0;
    ELSE
        internal := internal + 1;
    END_IF;
    count := internal;
END_FUNCTION_BLOCK

Using a Function Block

Function blocks must be instantiated as variables before use:

PROGRAM main
    VAR
        my_counter : Counter;
        value : INT;
    END_VAR

    my_counter(reset := FALSE);
    value := my_counter.count;
END_PROGRAM

Outputs are accessed using dot notation on the instance.

See Also