Declaring variables

Variable Declaration

In TwinCAT 3 PLC projects, you can declare variables in the following places:

The declaration editor is used for the variable declarations in global variable lists (GVL) and in the declaration part of POUs. Special editors or dialogs available for all other variants.

Declaring variables 1:

The properties of one or several variables can be influenced by adding pragmas. More detailed information can be found in section “Using pragmas”.

Syntax for variable declarations in POUs or global variable lists, if the text-based option is selected for the declaration editor:

<Identifier> {AT <Address>}:<Data type> {:=<Initialization>};

Variable initialization

The default initialization value for all declarations is 0. You can also specify user-defined initialization values for each variable and each data type in the declaration part.

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.

Sample 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

Sample 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: