P4038

Code

P4038

Message

Variable initializer expression does not reduce to a constant value

This error occurs when a VAR initializer’s constant expression (enabled by --allow-constant-initializer-expressions) does not fully reduce to a constant value — for example, because it references a variable that is not declared with the CONSTANT qualifier, or an undeclared identifier.

Example

The following code will generate error P4038:

PROGRAM main
VAR
    scale : LREAL := 2.0;
    d2r : LREAL := scale/180.0;
END_VAR
END_PROGRAM

scale is not a constant, so scale/180.0 cannot be evaluated at compile time. To fix this error, declare the referenced variable as a constant:

PROGRAM main
VAR CONSTANT
    scale : LREAL := 2.0;
END_VAR
VAR
    d2r : LREAL := scale/180.0;
END_VAR
END_PROGRAM

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.