Object ‘Function’

A function is a POU that supplies precisely one data element when executed and whose call in textual languages can occur as an operator in expressions. The data element can also be an array or a structure.

The object is added to the application or the project using the command Project ‣ Add object ‣ POU. In the Device tree or in the POUs view, function POUs have the suffix (FUN).

Attention

Functions have no internal status information, which means that functions do not save the values of their variables until the next call. Calls of a function with the same input variable values always supply the same output value. Therefore, functions may not use global variables and addresses!

The editor of a function consists of the declaration part and the implementation part.

The uppermost line of the declaration part contains the following declaration:

FUNCTION <function> : <data type>

Below that you declare the input and function variables.

The output variable of a function is the function name.

Attention

If you declare a local variable in a function as RETAIN, this has no effect! In this case CODESYS outputs a compiler error.

Attention

You cannot mix explicit and implicit parameter assignments in function calls in CODESYSV3. That means that you must use either only explicit or only implicit parameter assignments in function calls. The order of the parameter assignments when calling a function is arbitrary.

Calling a function

In ST you can use the call of a function as an operand in expressions.

In SFC you can use a function call only within step actions or transitions.

Examples

Function with declaration part and a line implementation code

../_images/5e04243b153dfff3c0a8640e0181fb1c

Function calls:

ST:

result := POU_Funct(5,3,22)

IL:

../_images/4b00b9201590f38ac0a8640e01486faa

FBD:

../_images/eb5acb621582f446c0a8640e001ba697

Functions with additional outputs

According to the IEC 61131-3 standard, functions can have additional outputs. The additional outputs are declared in the function between the keywords VAR_OUTPUT and END_VAR. The function is called in accordance with the following syntax:

<function> (<function output variable1> => <output variable 1>, <function output variable n> => <output variable n>)

Example

The function fun is defined with two input variables in1 and in2. The output variable of the function fun is written to the locally declared output variables loc1 and loc2.

fun(in1 := 1, in2 := 2, out1 => loc1, out2 => loc2);