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 by clicking Project ‣ Add Object ‣ POU . In the device tree or in the POUs view, function POUs have the (FUN) suffix.

Hint

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 top 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.

Hint

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

Hint

You cannot mix explicit and implicit parameter assignments in function calls in CODESYS V3. This means that you have to 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

Function calls:

ST:

result := POU_Funct(5,3,22);

AWL:

FBD:

Functions with additional outputs

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

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

Example

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

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

See also