P4058

Code

P4058

Message

Function call passes an expression that is not a variable to a VAR_IN_OUT parameter

This error occurs when a function call passes something other than a variable to a VAR_IN_OUT parameter. A VAR_IN_OUT parameter is passed by reference: the function reads and writes the caller’s variable, so the argument must be a variable. A literal or an expression has no storage the function could write to.

Example

The following code will generate error P4058:

FUNCTION Increment : DINT
    VAR_IN_OUT
        counter : DINT;
    END_VAR
    counter := counter + 1;
    Increment := counter;
END_FUNCTION

PROGRAM main
    VAR
        total : DINT;
        result : DINT;
    END_VAR
    result := Increment(total + 1);   (* Error: not a variable *)
END_PROGRAM

To fix this error, pass a variable:

result := Increment(total);

If the function should not change the caller’s value, declare the parameter in VAR_INPUT instead, which accepts any expression.

See Also

  • FUNCTION — FUNCTION declaration syntax

  • P4059 — a VAR_IN_OUT argument of the wrong type

References

  • IEC 61131-3 §2.5.1.1 (Edition 2), §6.6.1.2 (Edition 3)

Think IronPLC is wrong about this?

If you believe this diagnostic is incorrect, open an issue on GitHub with a small sample that demonstrates the problem.