Variables are used to identify elements with variable contents. Examples are inputs, outputs, or memory cells. A variable can be declared by an elementary or derived (user) data type. Part of the declaration may be information about the variable's initial value. Each declaration of program organisation unit (program, function block or function) should contain at least one declaration part to define variables used within the POU (Program Organisation Unit, or part of the code, as definition of a program, function, procedure etc.).
A declaration starts with the keyword VAR (for internal POU), or a keyword which starts with VAR_ :
A declaration ends with the keyword END_VAR. The variables are valid in the POU only in which they are declared (they are local here), and they are not accessible for the other POUs. An exception are variables to exchange parameters between a POU and its environment, which are input variables, variables wich can be written from the outside (VAR_INPUT), output variables (VAR_OUTPUT), which can be read from the outside, and variables for bidirectional access (VAR_IN_OUT).
Variables declared as VAR have their fixed positions in memory, and if the activation of a POU terminates, they keep the last value which is then used as default at the next POU activation.
However, there are variables with general (global) accessibility, VAR_GLOBAL. They are declared outside of the POU (usually before the program) and they are accessible for all POUs in the program. There may be the RETAIN qualificator after the VAR_GLOBAL keyword. It identifies variables which are backuped in a NVRAM memory. Another qualificator is CONSTANT to specify constant value of the variable.
Example:
VAR_GLOBAL RETAIN
GlobalBit BOOL;
GlobalByte BYTE := 10;
END_VAR
In the example above both variables are declared as global and retain. After switching the controller on (warm restart) the last value before switching off is restored. The bit (boolean) memory GlobalBit is not initiated, while GlobalByte is assigned the value of 10 after a cold start.
Every variable with the RETAIN flag is copied to the RETAIN area after each communication cycle (in case of more tasks, after every task). The RETAIN area is a separate part of memory which keeps its contents even after power outage. In case of warm restarts (eg. power dropout), all RETAIN variables are set to the values saved in the RETAIN area before the program is launched. If the PLC is cold-restarted, all variables including RETAIN variables are initialised to the values saved in the compiled program code. The RETAIN variables may be used for e.g. operating hour counters and similar variables which values should be preserved even after power outage or similar event. At RETAIN and NON_RETAIN combinations at embedded types, the highest flag is valid, i.e. the programmer always can override the type setting and set RETAIN or NON_RETAIN at individual variables. In this case, the settings of embedded entities is not relevant, all of them are RETAIN or NON_RETAIN. Note that except for variables and types introduced in the user program it is not advised to override library-defined types. If you think this is a good idea, please contact the author of the procedure and consult the intended usage. Extended usage of RETAIN variables may affect performance of the controller and should always be considered carefully.
Similar properties may be defined for VAR_EXTERNAL variables which originate at external resources.
Example 2:
Variable definition within a project.