Variable declaration: Where and how?
You can declare variables at the following locations:
-
Declaration part of a POU
The “Declare Variable” dialog helps you with this.
Hint: If you define a variable in the tabular declaration editor, the correct syntax is automatically produced.
-
Declaration part of the GVL or NVL editor
-
I/O mapping configuration of an I/O device object
Syntax
( <pragma> )* <scope> ( <type qualifier> )? <identifier> (AT <address> )? : <data type> ( := <initial value> )? ; END_VAR
Declaration |
||
<pragma> |
Pragma (none, one, or multiple) Note: By adding a pragma, you can affect the behavior and the properties of one or more variables. |
See also |
|
Scope
|
See also |
|
Type qualifier
|
|
|
Identifier, variable name Note: The rules listed in the chapter "Identifiers" must be followed without exception when assigning an identifier. In addition, you will find recommendations for uniform naming. |
See also |
|
Assignment of an address in the input, output, or flag memory range (I, Q, or M)
Example
|
|
|
Data type
|
|
|
Initial value
|
|
|
Optional |
|
|
Optional repetition |
Example
GVL
{attribute 'qualified_only'} {attribute 'linkalways'} VAR_GLOBAL CONSTANT g_ciMAX_A : INT := 100; g_ciSPECIAL : INT := g_ciMAX_A - 10; END_VAR
GVL_CONFIG
{attribute 'qualified_only'} VAR_CONFIG // Generated instance path of variable at incomplete address PLC_PRG.fbDoItNow.XLOCINPUT AT %I*: BOOL := TRUE; END_VAR
FB_DoIt (FB)
METHOD METH_Last : INT VAR_INPUT iVar : INT; END_VAR VAR_INST iLast : INT := 0; END_VAR METH_Last := iLast; iLast := iVar; FUNCTION_BLOCK FB_DoIt VAR_INPUT wInput AT %IW0 : WORD; (* Input variable *) END_VAR VAR_OUTPUT wOutput AT %QW0 : WORD; (* Output variable *) END_VAR VAR_IN_OUT aData_A : ARRAY[0..1] OF DATA_A; // Formal variable END_VAR VAR_EXTERNAL GVL.g_ciMAX_A : INT; // Declared in object GVL END_VAR VAR_STAT iNumberFBCalls : INT; END_VAR VAR iCounter: INT; xLocInput AT %I* : BOOL := TRUE; // VAR_CONFIG END_VAR iNumberFBCalls := iNumberFBCalls + 1;
PLC_PRG (PRG)
PROGRAM PLC_PRG VAR iLoop: INT; iTest: INT; fbDoItNow : FB_DoIt; iTest_200: INT; aData_Now : ARRAY[0..1] OF DATA_A := [(iA_1 := 1, iA_2 := 10, dwA_3 := 16#00FF),(iA_1 := 2, iA_2 := 20, dwA_3 := 16#FF00)]; END_VAR iTest := GVL.g_ciMAX_A; iTest_200 := 2 * GVL.g_ciMAX_A; fbDoItNow(aData_A := aData_Now); FOR iLoop := 0 TO GVL.g_ciSPECIAL DO ; END_FOR
Variable initialization
The standard initialization value for all declarations is 0
. In the declaration part you can also specify user-defined initialization values
for each variable and each data type.
The user-defined initialization starts with the assignment operator :=
and consists of any valid expression of the programming language ST (structured text).
You thus define the initialization value with the help of constants, other variables
or functions. If you use a variable, you must also initialize it.
Examples
VAR var1:INT := 12; // initialization value 12 x : INT := 13 + 8; // initalization value defined by an expression of constants y : INT := x + fun(4); // initialization value defined by an expression, // that contains a function call; notice the order! z : POINTER TO INT := ADR(y); // not described in the standard IEC61131-3: // initialization value defined by an adress function; // Notice: In this case the pointer will not be initialized // during an Online Change *) END_VAR
Notes on the order of initialization
From compiler version 3.5.3.40, variables in a function block are initialized in the following order: firstly, all constants in accordance with the order of their declarations, then all other variables in accordance with the order of their declarations.




NOTICE

From compiler version 3.3.2.0, variables from global variable lists are always initialized before the local variables of a POU.