P2042¶
- Code
P2042
- Message
Return type is not declared
This error occurs when a function or method declares a return type that has not been declared.
The return type decides how much storage the result needs and which operations a caller may apply to it. Without a declaration, the compiler cannot know either, so it reports the name here rather than letting every call site fail with a less specific error. A misspelled type name, or a type that lives in a file that is not part of the project, are the usual causes.
Example¶
The following code will generate error P2042:
FUNCTION GetMode : E_Mode (* Error: E_Mode is not declared *)
GetMode := 0;
END_FUNCTION
The function GetMode returns E_Mode, but no type with that name has been declared.
To fix this error, declare the type:
TYPE
E_Mode : (Idle, Running);
END_TYPE
FUNCTION GetMode : E_Mode (* Correct: E_Mode is declared *)
GetMode := Idle;
END_FUNCTION
Alternatively, return a built-in type:
FUNCTION GetMode : INT (* Correct: INT is a built-in type *)
GetMode := 0;
END_FUNCTION
See Also¶
FUNCTION —
FUNCTIONMETHOD —
METHODP2008 — Cannot determine kind of type identifier
P2013 — Array element type is not declared
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.