Inhalte

ST/ExST Practices: VAR_OUTPUT

Table of Contents

Output Parameter Order Preservation Rule

If there are existing VAR_OUTPUT parameters, new output parameters must only be added at the end of the VAR_OUTPUT block. This is because, in cases where a method or function does not have an explicit return value, TwinCAT 3 automatically returns the first VAR_OUTPUT parameter. Since it is uncertain whether this parameter is already being used in the software, altering the order could introduce potential errors.

Beware of the void!

This code is dangerous:

fooculate
METHOD foo
VAR_OUTPUT
    bar :INT;
END_VAR

As the return type of foo will be int. If you change the method to look like this:

fooculate
METHOD foo
VAR_OUTPUT
    baz :INT;
    bar :INT;
END_VAR

the return type will still be int, but a different value is returned, just by ordering the outputs differently. This can lead too hard to find errors which are quickly made. Instead, a better practice would be:

fooculate
METHOD foo :INT
VAR_OUTPUT
    baz :INT;
    bar :INT;
END_VAR

bar := ...;

foo := bar;

This allows the same usage while avoiding errors by ordering or adding variables and makes the code unambiguous even if you do not know that the first var_output will be chosen as output if the method/function is void.