P4027¶
- Code
P4027
- Message
Function return type does not match assignment destination type
This error occurs when a function’s return type does not match the type of the variable
being assigned to. IronPLC allows implicit widening between integer types (for example,
a function returning SINT assigned to a DINT variable), but rejects other type
mismatches such as real to integer or narrowing conversions.
For the full set of allowed implicit conversions, see Type Conversions.
Example¶
The following code will generate error P4027:
FUNCTION GET_VALUE : REAL
VAR_INPUT
A : REAL;
END_VAR
GET_VALUE := A;
END_FUNCTION
PROGRAM main
VAR
result : INT;
x : REAL;
END_VAR
result := GET_VALUE(x); (* Error: REAL return assigned to INT variable *)
END_PROGRAM
The function GET_VALUE returns REAL, but result is INT.
To fix this error, use an explicit type conversion:
result := REAL_TO_INT(GET_VALUE(x));
Standard library functions¶
The check applies to the standard library as well as to functions you write. A conversion function is the most common way to hit it, because its name says which type it returns:
PROGRAM main
VAR
result : INT;
x : INT;
END_VAR
result := INT_TO_REAL(x); (* Error: REAL return assigned to INT variable *)
END_PROGRAM
Here INT_TO_REAL converts to REAL, so the result does not fit result.
Either declare result as REAL, or convert in the direction you meant.
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.