Skip to main content

Variable Declaration

Variable declaration: Where and how?

You can declare variables at the following locations:

For more information, see: Shortcuts for Variable Declaration

Syntax

<pragma>

<scope> <type qualifier>

    <identifier> AT <address> : <data type> := <initial value> ;

END_VAR

<pragma>

Repeated optional (never, once, or multiple times)

Pragma

A pragma is a compiler statement (for example to achieve optimizations such as memory requirements or runtime improvements).

<scope>

Required

Scope

  • VAR

  • VAR_CONFIG

    Note: If variables with incomplete address information are declared in function blocks (for example, AT %I*), then the variables in the variable declaration VAR_CONFIG have to be completely declared. You can access these variables in a local instance only when this is done.

  • VAR_EXTERNAL

  • VAR_GLOBAL

  • VAR_INPUT

  • VAR_INST

  • VAR_IN_OUT

  • VAR_OUTPUT

  • VAR_STAT

  • VAR_TEMP

<type qualifier>

Optional

Type qualifier

  • CONSTANT

  • RETAIN

  • PERSISTENT

<identifier>

Required

Identifier, variable name

Note: The rules listed in the chapter "Identifier Designation" have to be followed without exception when assigning an identifier. In addition, you will find recommendations for uniform naming.

AT %<address>

Optional

The address is made up as follows:

<memory area> <optional size prefix> <memory position>

The memory area is subdivided into input memory area, output memory area, or flag memory area (I, Q or M).

Example

  • AT %I* // Incomplete address

  • AT %I7.5

  • AT %IW0

  • AT %QX7.5

  • AT %MD48

<data type>

Required

Data Type

  • Elementary data type

  • Custom Data Type

  • Function Block

<initial value>

Optional

Initial value; initialization as a literal, variable, or expression

Example 12. Example

Global variable list GVL

{attribute 'qualified_only'}
{attribute 'linkalways'}
VAR_GLOBAL CONSTANT
    g_ciMAX_A : INT := 100;
    g_ciSPECIAL : INT := g_ciMAX_A - 10;
END_VAR

Configuration variables 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

Function block FB_DoIt

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;

IEC program PLC_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