Declaring variables
Variable Declaration
You can declare variables in the following places:
- Declaration part of a programming object
- GVL editor
The dialog Auto Declare supports you with the variable declaration.
Syntax
( <pragma> )*
<scope> ( <type qualifier> )?
<identifier> (AT <address> )? : <data type> ( := <initial value> )? ;
END_VAR
<pragma> (optional) | Pragma (no at all, once or several times) The properties of one or several variables can be influenced by adding a pragma. | See also |
<scope> | Scope
| See also |
<type qualifier> (optional) | Type qualifier
| See also |
<identifier> | Identifier, variable name It is important to follow the rules listed in section “Identifiers” when assigning the identifiers. Additional harmonization conventions can be found in section “Identifiers/Names”. | See also
|
AT <address> (optional) | Assignment of an address in the input, output, or flag memory area (I, Q, or M) Example: AT %I*, AT %Q* | See also |
<data type> | Data type
| See also |
<initial value> (optional) | Initial value
| See also
|
( ... )? | Optional |
|
( … )* | Optional repetition |
|
Variable initialization
The standard initialization value for all declarations is 0. In the declaration part you can specify user-defined initialization values for each variable and each data type.
The user-defined initialization starts with the allocation operator := and consists of a valid expression in the programming language ST (Structured Text). The initialization value is thus defined with the aid of constants, other variables or functions. If you use a variable, this also has to be initialized.
Example 1:
VAR
nVar1 : INT := 12; // initialization value 12
nVar2 : INT := 13 + 8; // initialization value defined by an expression of constants
nVar3 : INT := nVar2 + F_Fun(4); //initialization value defined by an expression that contains a function call; notice the order!
pSample : POINTER TO INT := ADR(nVar1); //not described in the standard IEC61131-3: initialization value defined by an adress function; Notice: the pointer will not be initialized during an Online Change
END_VAR
Example 2:
In the following sample, an input variable and a property are initialized by a function block that has an FB_init method with an additional parameter.
Function block FB_Sample:
FUNCTION_BLOCK FB_Sample
VAR_INPUT
nInput : INT;
END_VAR
VAR
nLocalInitParam : INT;
nLocalProp : INT;
END_VAR
Method FB_Sample.FB_init:
METHOD FB_init : BOOL
VAR_INPUT
bInitRetains : BOOL; // if TRUE, the retain variables are initialized (warm start / cold start)
bInCopyCode : BOOL; // if TRUE, the instance afterwards gets moved into the copy code (online change)
nInitParam : INT;
END_VAR
nLocalInitParam := nInitParam;
Property FB_Sample.nMyProperty and the associated Set function:
PROPERTY nMyProperty : INT
nLocalProp := nMyProperty;
Program MAIN:
PROGRAM MAIN
VAR
fbSample : FB_Sample(nInitParam := 1) := (nInput := 2, nMyProperty := 3);
aSample : ARRAY[1..2] OF FB_Sample[(nInitParam := 4), (nInitParam := 7)]
:= [(nInput := 5, nMyProperty := 6), (nInput := 8, nMyProperty := 9)];
END_VAR
Initialization result:
- fbSample
- nInput = 2
- nLocalInitParam = 1
- nLocalProp = 3
- aSample[1]
- nInput = 5
- nLocalInitParam = 4
- nLocalProp = 6
- aSample[2]
- nInput = 8
- nLocalInitParam = 7
- nLocalProp = 9
See also:
- Reference Programming > Data types
- Reference Programming > Variable types and special variables
- Reference Programming > Operands > Addresses
- Reference Programming > Identifier
- TwinCAT 3 programming conventions > Identifier/name