Variables
Variables are declared in a variable section. A variable can be declared with
- an elementary data type,
- a derived type (user-defined),
- a reference type, or
- a type defined inline within the declaration.
A variable declaration consists of
- a list of variable names,
- a
:(colon), and - a data type, with optional variable-specific initialization.
VAR myVar1, myVar2 : INT; // two variables with an elementary type myTyped : MyType; // a previously user-defined type myArray : ARRAY[1..8] OF REAL; // an inline (immediate) typeEND_VARVariable sections
Section titled “Variable sections”Each section is opened by its keyword and closed by END_VAR. Which sections are
valid depends on the kind of POU (program, function, function block, …).
| Section | Purpose |
|---|---|
VAR | Internal variables, local to the entity. |
VAR_INPUT | Inputs, supplied by the caller; not assigned inside the entity. |
VAR_OUTPUT | Outputs, produced by the entity and read by the caller. |
VAR_IN_OUT | In/out parameters passed by reference; readable and writable. |
VAR_TEMP | Temporary variables, re-initialized on every invocation. |
VAR_EXTERNAL | Declares access to a VAR_GLOBAL variable defined elsewhere. |
VAR_GLOBAL | Global variables available throughout the project. |
VAR_ACCESS | Access paths binding variables to external/HMI data (configuration). |
VAR_CONFIG | Per-instance initial values assigned in the configuration. |
FUNCTION_BLOCK MixerVAR_INPUT setpoint : REAL;END_VARVAR_OUTPUT actual : REAL;END_VARVAR_IN_OUT tank : TankState; // passed by reference, modified in placeEND_VARVAR pid : PIDController; // internal instance, keeps state between cyclesEND_VARVAR_TEMP error : REAL; // recomputed each callEND_VAREND_FUNCTION_BLOCKQualifiers
Section titled “Qualifiers”Qualifiers placed after the section keyword change how the declared variables behave.
| Qualifier | Applies to | Meaning |
|---|---|---|
CONSTANT | VAR, VAR_INPUT, VAR_GLOBAL, VAR_EXTERNAL | Read-only; the value cannot be assigned after initialization. |
RETAIN | VAR, VAR_GLOBAL | Value is retained across a warm restart. |
NON_RETAIN | VAR, VAR_GLOBAL | Value is explicitly not retained; re-initialized on restart. |
VAR CONSTANT PI : REAL := 3.14159;END_VAR
VAR RETAIN cycleCount : DINT; // survives a warm restartEND_VAREdge declarations
Section titled “Edge declarations”A Boolean variable can be declared as an edge-triggered flag with R_EDGE
(rising edge) or F_EDGE (falling edge). The variable becomes TRUE for one
cycle on the corresponding transition of its input.
VAR_INPUT startBtn : BOOL R_EDGE; // rising-edge input stopBtn : BOOL F_EDGE; // falling-edge inputEND_VARLocated variables (AT %…)
Section titled “Located variables (AT %…)”A variable can be mapped to a directly-represented (physical or memory) address
with AT, followed by a % location.
VAR sensor AT %IX0.0 : BOOL; // input bit holding AT %MW100 : WORD; // memory wordEND_VARThe location prefix selects the area — I (input), Q (output), M
(memory) — and the size prefix selects the width — X (bit), B (byte),
W (word), D (double word), L (long word).
Initialization rules
Section titled “Initialization rules”The initial value of a variable is determined, in order of precedence:
- The user-defined value given in the
VARdeclaration with:=(highest precedence). - The user-defined value given in the
TYPEdeclaration of its data type with:=. NULL, if the variable is a reference.- The default initial value of the underlying elementary type (lowest precedence).
VAR a : INT; // 0 (elementary default) b : INT := 5; // 5 (declaration initializer) c : Counter; // uses the TYPE's := default, if any r : REF_TO Motor; // NULLEND_VAR