Declaring Variables

Variable declaration: where and how

In CODESYS projects you can declare variables in the following places:

  • Declaration part of a POU
  • Dialog box Auto Declare, if a POU has the focus
  • DUT, GVL or NVL editor
  • I/O mapping configuration of an I/O device object

The variable declarations in global variable lists (GVL, NVL) and in the declaration part of POUs are carried out in the declaration editor. Special editors or dialog boxes are available for all other variants.

Hint

By adding pragmas you can affect the behavior and the properties of one or more variables. More precise information can be found in the chapter “Use of pragmas”.

Syntax for the variable declaration in POUs or global variable lists, if textual display is selected for the declaration editor:

<identifier> {AT <address>}:<data type> {:=<initialisation>};
  • <identifier>: Name of the variable. The rules listed in the chapter “Identifiers” must be followed without exception when assigning an identifier. In addition, this chapter contains recommendations for standardizing the assignment of names.
  • {AT <address>} (optional): You can directly bind a variable to a certain address with the help of the keyword AT.
  • <data type>
  • {:=<initialization>} (optional)

Note

In function blocks you can also declare variables with incomplete address specifications. In order to be able to use such a variable in a local instance, however, there must be a corresponding entry for this variable in the “variable configuration”.

If you define a variable in the tabular declaration editor, the correct syntax is automatically produced.

See also

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: the pointer will not be initialized  diesem Fall,
during an Online Change *)
END_VAR

Notes on the order of initialization

Note

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.

Hint

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

See also