P4018¶
- Code
P4018
- Message
Function call has wrong number of arguments
This error occurs when calling a function with the wrong number of arguments,
or when a method call does not supply every VAR_INPUT and VAR_IN_OUT
of the method.
Example¶
The following code will generate error P4018:
FUNCTION ADD_INTS : INT
VAR_INPUT
A : INT;
B : INT;
END_VAR
ADD_INTS := A + B;
END_FUNCTION
FUNCTION_BLOCK CALLER
VAR
result : INT;
END_VAR
result := ADD_INTS(1); (* Error: Expected 2 arguments, got 1 *)
END_FUNCTION_BLOCK
The function ADD_INTS expects 2 input arguments (A and B), but only 1 was provided.
To fix this error, provide the correct number of arguments:
FUNCTION ADD_INTS : INT
VAR_INPUT
A : INT;
B : INT;
END_VAR
ADD_INTS := A + B;
END_FUNCTION
FUNCTION_BLOCK CALLER
VAR
result : INT;
END_VAR
result := ADD_INTS(1, 2); (* Now valid: 2 arguments provided *)
END_FUNCTION_BLOCK
A method call names every input of the method, because a method keeps nothing between calls. An initial value on an input is not a default:
FUNCTION_BLOCK SCALER
METHOD SCALE : INT
VAR_INPUT
V : INT;
K : INT := 2;
END_VAR
SCALE := V * K;
END_METHOD
END_FUNCTION_BLOCK
PROGRAM MAIN
VAR
s : SCALER;
result : INT;
END_VAR
result := s.SCALE(V := 3); (* Error: K is not supplied *)
END_PROGRAM
To fix this error, supply every input: s.SCALE(V := 3, K := 2).
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.