General Information

Overview

You can declare variables:

The kind (in the tabular declaration editor it is named Scope) of the variables to be declared is specified by the keywords embracing the declaration of one or several variables. In the textual declaration editor, the common variable declaration is embraced by VAR and END_VAR.

For further variable declaration scopes, refer to:

  • VAR_INPUT

  • VAR_OUTPUT

  • VAR_IN_OUT

  • VAR_GLOBAL

  • VAR_TEMP

  • VAR_STAT

  • VAR_INST

  • VAR_EXTERNAL

  • VAR_CONFIG

The variable type keywords may be supplemented by attribute keywords.

Example: RETAIN (VAR_INPUT RETAIN)

Syntax

Syntax for variable declaration:

<Identifier> {AT <address>}:<data type> {:=<initialization>};

The parts in braces {} are optional.

Identifier

The identifier is the name of a variable.

Consider the following facts when defining an identifier.

  • no spaces or special characters allowed

  • no case-sensitivity: VAR1, Var1 and var1 are all the same variable

  • recognizing the underscore character: A_BCD and AB_CD are considered two different identifiers. Do not use more than one underscore character in a row.

  • unlimited length

  • no keyword name allowed, such as VAR_GLOBAL

  • multiple use (see next paragraph)

Also, consider the chapter Best Practices for the Naming of Identifiers.

Multiple Use of Identifiers (Namespaces)

The following outlines the regulations concerning the multiple use of identifiers:

  • Duplicate use of identifiers is not allowed locally.

  • Multiple use of an identifier is allowed globally: a local variable can have the same name as a global one. In this case, the local variable within the POU will have priority.

  • A variable defined in a global variable list (GVL) can have the same name as a variable defined in another global variable list (GVL). In this context, consider the following IEC 61131-3 extending features:

    • Global scope operator: an instance path starting with a dot (.) opens a global scope. So, if there is a local variable, for example ivar, with the same name as a global variable, .ivar refers to the global variable.

    • You can use the name of a global variable list (GVL) as a namespace for the included variables. You can declare variables with the same name in different global variable lists (GVL). They can be accessed specifically by preceding the variable name with the list name.

      Example

      globlist1.ivar := globlist2.ivar;

      (* ivar from globlist2 is copied to ivar in GVL globlist1 *)

    • Variables defined in a global variable list of an included library can be accessed according to syntax <library namespace>.<name of GVL>.<variable>.

      Example:

      globlist1.ivar := lib1.globlist1.ivar

      (* ivar from globlist1 in library lib1 is copied to ivar in GVL globlist1 *)

  • For a library also, a namespace is defined when it gets included via the Library Manager. So you can access a library module or variable by <library namespace>.<modulename|variablename>. Consider that, in case of nested libraries, the namespaces of all libraries concerned have to be stated successively.

    Example: If Lib1 is referenced by Lib0, the module fun being part of Lib1 is accessed by Lib0.Lib1.fun:

    ivar := Lib0.Lib1.fun(4, 5); (* return value of fun is copied to variable ivar in the project *)

    NOTE: Once the checkbox Publish all IEC symbols to that project as if this reference would have been included there directly. has been activated within the Properties dialog box of the referenced library Lib, the module fun may also be accessed directly via Lib0.fun.
  • Variables declared in GVLs or POUs of the Global node of the Applications tree can be accessed by prefixing them with the operator "__POOL.".

AT <address>

You can link the variable directly to a definite address using the keyword AT.

In function blocks, you can also specify variables with incomplete address statements. In order that such a variable can be used in a local instance, an entry has to exist for it in the variable configuration.

Type

Valid data type, optionally extended by an :=< initialization>.

Pragma Instructions

Optionally, you can add pragma instructions in the declaration part of an object in order to affect the code generation for various purposes.

Hints

Automatic declaration of variables is also possible.

For faster input of the declarations, use the shortcut mode.