Using Functions

Elementary functions are provided in the form of libraries. The logic of the functions is created in the programming language C and may not be modified in the IL editor.

Functions have no internal states. If the input values are the same, the value on the output is the same every time the function is called. For example, the addition of two values always gives the same result. With some elementary functions, the number of inputs can be increased.

Elementary functions only have one return value (output).

Parameters

"Inputs" and one "output" are required to transfer values to or from a function. These are called formal parameters.

The current process states are transferred to the formal parameters. These are called actual parameters.

The following can be used as actual parameters for function inputs:

  • Variable

  • Address

  • Literal

The following can be used as actual parameters for function outputs:

  • Variable

  • Address

The data type of the actual parameters must match the data type of the formal parameters. The only exceptions are generic formal parameters whose data type is determined by the actual parameter.

When dealing with generic ANY_BIT formal parameters, actual parameters of the INT or DINT (not UINT and UDINT) data types can be used.

This is a supplement to IEC 61131-3 and must be enabled explicitly.

Example:

Allowed:

AND (AnyBitParam := IntVar1, AnyBitParam2 := IntVar2)

Not allowed:

AND_WORD (WordParam1 := IntVar1, WordParam2 := IntVar2)

(In this case, AND_INT must be used.)

AND_ARRAY_WORD (ArrayInt, ...)

(In this case an explicit type conversion must be carried out using INT_ARR_TO_WORD_ARR (...).

Not all formal parameters must be assigned a value for formal calls. The formal parameter types that must be assigned a value are in the following table:

Parameter type

EDT

STRING

ARRAY

ANY_ARRAY

IODDT

STRUCT

FB

ANY

Input

-

-

-

-

+

-

+

-

VAR_IN_OUT

+

+

+

+

+

+

/

+

Output

-

-

-

-

-

-

/

-

+ Actual parameter required

- Actual parameter not required, it's the general rule, but there are exceptions for some FFBs, for instance when some parameters are used to characterize the information we want to be given by the FFB.

/ not applicable

If no value is assigned to a formal parameter, the initial value will be used when the function is executed. If no initial value has been defined, the default value (0) is used.

Programming Notes

Attention should be paid to the following programming notes:

  • Functions are only executed if the input EN=1 or the EN input is not used (see also EN and ENO).

  • All generic functions are overloaded. This means the functions can be called with or without entering the data type.

    E.g.

    LD i1

    ADD i2

    ST i3

    is identical to

    LD i1

    ADD_INT i2

    ST i3

  • In contrast to ST, functions in IL cannot be nested.

  • There are two ways of invoking a function:

    • Formal call (calling a function with formal parameter names)

    • Informal call (calling a function without formal parameter names)

Formal Call

With this type of call (call with formal parameter names), the function is called using an instruction sequence consisting of the function name, followed by the bracketed list of value assignments (actual parameters) to the formal parameters. The order in which the formal parameters are listed is not significant. The list of actual parameters may be wrapped immediately following a comma. After executing the function the result is loaded into the accumulator and can be stored using ST.

EN and ENO can be used for this type of call.

Calling a function with formal parameter names:

or

LIMIT (
MN:=0,
IN:=var1,
MX:=var2
)
ST out

Calling the same function in FBD:

With formal calls, values do not have to be assigned to all formal parameters (see also Parameter).

LIMIT (MN:=0, IN:=var1)
ST out

Calling the same function in FBD:

Informal Call

With this type of call (call without formal parameter names), the function is called using an instruction sequence made up by loading the first actual parameter into the accumulator, followed by the function name and an optional list of actual parameters. The order in which the actual parameters are listed is significant. The list of actual parameters cannot be wrapped. After executing the function the result is loaded into the accumulator and can be stored using ST.

EN and ENO cannot be used for this type of call.

Calling a function with formal parameter names:

Calling the same function in FBD:

NOTE: Note that when making an informal call, the list of actual parameters cannot be put in brackets. IEC 61133-3 requires that the brackets be left out in this case to illustrate that the first actual parameter is not a part of the list.

Invalid informal call for a function:

If the value to be processed (first actual parameter) is already in the accumulator, the load instruction can be omitted.

LIMIT B,C
ST result

If the result is to be used immediately, the store instruction can be omitted.

LD A
LIMIT_REAL B,C
MUL E

If the function to be executed only has one input, the name of the function is not followed by a list of actual parameters.

Calling a function with one actual parameter:

Calling the same function in FBD:

EN and ENO

With all functions an EN input and an ENO output can be configured.

If the value of EN is equal to "0" when the function is called, the algorithms defined by the function are not executed and ENO is set to "0".

If the value of EN is equal to 1 when the function is called, the algorithms defined by the function are executed. After the algorithms have been executed successfully, the value of ENO is set to "1". If an error occurred while executing the algorithms, ENO is set to "0".

If the EN pin is not assigned a value, when the FFB is invoked, the algorithm defined by the FFB is executed (same as if EN equals to "1").

If ENO is set to "0" (caused when EN=0 or an error occurred during execution), the output of the function is set to "0".

The output behavior of the function does not depend on whether the function was called up without EN/ENO or with EN=1.

If EN/ENO are used, the function call must be formal.

LIMIT (EN:=1, MN:=0, IN:=var1, MX:=5, ENO=>var2)
ST out

Calling the same function in FBD: