Variables in user functions are used to assist in calculations, and/or serve as the function parameters.
Variables as Parameters
Variables in a user function that are declared with the VAR_INPUT, VAR_OUTPUT, and VAR_IN_OUT keywords make up the function's parameters. The order in which the parameters are declared in the user function is the order in which the parameters must be entered when calling that user function.
Input Parameters (VAR_INPUT)
Input parameters can only be read from. Values cannot be assigned to input parameters.
Output Parameters (VAR_OUTPUT)
Output parameters can only be assigned to. That is, output parameters can only be used on the left-hand side of assignment expressions.
Input/Output Parameters (VAR_IN_OUT)
Input/output parameters can be read and written.
Variables as Local Variables
Variables declared with the VAR keyword are variables that can be used locally in the user function to assist in calculations.
Declaring Variables and Parameters
Use the format in the example below to declare variables in user functions. Keep in mind that the order in which the parameters (VAR_INPUT, VAR_OUTPUT, and VAR_IN_OUT) are declared in the user function is the order in which the parameters must be entered when calling that user function.
Variables can be of any data type, and can be fixed-length arrays.
Local variables (VAR), output parameters (VAR_OUTPUT), and input/output parameters (VAR_IN_OUT) can optionally be initialized upon declaration.
Examples
VAR_INPUT
MyInputVar : REAL;
MyOtherInputVar : DINT;
YourVar : Array [0..4] OF REAL;
END_VAR
VAR_IN_OUT
MyInOutVar : REAL;
END_VAR
VAR_OUTPUT
MyOutputVar : REAL :=4;
MyArray : Array [0..3] OF REAL := [10, 10, 0];
END_VAR
VAR
MyVar : REAL := 100;
YourVar : Array [0..9] OF DINT;
END_VAR
Declaring Arrays
Array bounds are specified in brackets [], with two intermediate periods (..). The array bounds can be any integer value, including negative values. For most applications, the lower bound is zero. The maximum length of an array in a user function is 32 elements.
Parentheses can be used a repetition factor when assigning an initial value. The value preceding the parentheses specifies the number of repetitions. For example, [4(0)] is equivalent to [0,0,0,0], and [1,3(99),1] is equivalent to [1,99,99,99,1].
Example
This declaration creates an output array of length 5, with initial values of [-1,1,0,0,0].
VAR_OUTPUT
MyArray : Array [0..4] OF REAL := [-1,1, 3(0)];
END_VAR
See Also
User Functions | Example User Functions
Copyright © 2024 Delta Computer Systems, Inc. dba Delta Motion