This chapter provides further information on the following variable types:
oVAR local variables
oVAR_INPUT input variables
oVAR_OUTPUT output variables
oVAR_IN_OUT input and output variables
oVAR_GLOBAL global variables
oVAR_TEMP temporary variables
oVAR_STAT static variables
oVAR_EXTERNAL external variables
oVAR_INST instance variables
Between the keywords VAR and END_VAR, all local variables of a POU are declared. These have no external connection; in other words, they cannot be written from the outside.
Consider the possibility of adding an attribute to VAR.
Example
VAR
iLoc1:INT; (* 1. Local Variable*)
END_VAR
Between the keywords VAR_INPUT and END_VAR, all variables are declared that serve as input variables for a POU. This means that at the call position, the value of the variables can be provided along with a call.
Consider the possibility of adding an attribute.
Example
VAR_INPUT
iIn1:INT (* 1. Inputvariable*)
END_VAR
Between the keywords VAR_OUTPUT and END_VAR, all variables are declared that serve as output variables of a POU. This means that these values are carried back to the POU that makes the call.
Consider the possibility of adding an attribute to VAR_OUTPUT.
Example
VAR_OUTPUT
iOut1:INT; (* 1. Outputvariable*)
END_VAR
Output variables in functions and methods:
According to IEC 61131-3 draft 2, functions (and methods) can have additional outputs.You can assign them in the call of the function as shown in the following example.
Example
fun(iIn1 := 1, iIn2 := 2, iOut1 => iLoc1, iOut2 => iLoc2);
Input and Output Variables - VAR_IN_OUT
Between the keywords VAR_IN_OUT and END_VAR, all variables are declared that serve as input and output variables for a POU.
NOTE: With variables of IN_OUT type, the value of the transferred variable is changed (transferred as a pointer, Call-by-Reference). This means that the input value for such variables cannot be a constant. For this reason, even the VAR_IN_OUT variables of a function block cannot be read or written directly from outside via <FBinstance>.<InOutVariable>.
NOTE: Do not assign bit-type symbols (such as %MXaa.b or BOOL variables that are located on such a bit-type address) to BOOL-type VAR_IN_OUT parameters of function blocks. If any such assignments are detected, they are reported as a detected Build error in the Messages view.
Example
VAR_IN_OUT
iInOut1:INT; (* 1. Inputoutputvariable *)
END_VAR
You can declare normal variables, constants, external, or remanent variables that are known throughout the project as global variables. To declare global variables, use the global variable lists (GVL). You can add a GVL by executing the Add Object command (by default in the Project menu).
Declare the variables locally between the keywords VAR_GLOBAL and END_VAR.
Consider the possibility of adding an attribute to VAR_GLOBAL.
A variable is recognized as a global variable by a preceding dot, for example, .iGlobVar1.
For detailed information on multiple use of variable names, the global scope operator dot (.) and name spaces refer to the chapter Global Scope Operator.
Global variables can only be declared in global variable lists (GVLs). They serve to manage global variables within a project. You can add a GVL by executing the Add Object command (by default in the Project menu).
NOTE: A variable defined locally in a POU with the same name as a global variable will have priority within the POU.
NOTE: Global variables are initialized before local variables of POUs.
Temporary Variables - VAR_TEMP
This feature is an extension to the IEC 61131-3 standard.
Temporary variables get (re)initialized at each call of the POU. VAR_TEMP declarations are only possible within programs and function blocks. These variables are also only accessible within the body of the program POU or function block.
Declare the variables locally between the keywords VAR_TEMP and END_VAR.
NOTE: You can use VAR_TEMP instead of VAR to reduce the memory space needed by a POU (for example inside a function block if the variable is only used temporarily).
This feature is an extension to the IEC 61131-3 standard.
Static variables can be used in function blocks, methods, and functions. Declare them locally between the keywords VAR_STAT and END_VAR. They are initialized at the first call of the respective POU.
Such as global variables, static variables do not lose their value after the POU in which they are declared is left. They are shared between the POUs they are declared in (for example, several function block instances, functions or methods share the same static variable). They can be used, for example, in a function as a counter for the number of function calls.
Consider the possibility of adding an attribute to VAR_STAT.
External Variables - VAR_EXTERNAL
These are global variables which are imported into the POU.
Declare them locally between the keywords VAR_EXTERNAL and END_VAR and in the global variable list (GVL). The declaration and the global declaration have to be identical. If the global variable does not exist, a message will display.
NOTE: It is not necessary to define variables as external. These keywords are provided in order to maintain compatibility to IEC 61131-3.
Example
VAR_EXTERNAL
iVarExt1:INT; (* 1st external variable *)
END_VAR
If you declare a variable of a method as an instance variable by using the VAR_INST attribute, then this variable is not stored on the method stack but on the stack of the function block instance. It therefore behaves like other variables of the function block instance and is not reinitialized when the method is called.
VAR_INST variables are only allowed in methods. You can access such variables within the method only. Attributes such as CONST, RETAIN are not allowed in the declaration. The values of the variables can be monitored in the declaration part of the method.
Example
METHOD meth_last : INT
VAR_INPUT
iVar : INT;
END_VAR
VAR_INST
iLast : INT := 0;
END_VAR
meth_last := iLast;
iLast := iVar;