Variable Declaration

Help Contents

Introduction

A variable is a named unit of data that is assigned a value.

You can declare variables:

Syntax

All variable declarations begin with a keyword, which defines the storage of the variables in the block:

KeywordUsage
VAR Declare local variables in:
VAR_INPUT Declare input parameters to:
VAR_OUTPUT Declare output parameters from:
VAR_IN_OUT Declare parameters passed by reference to:
VAR_TEMP Declare a temporary variable in:
VAR_GLOBAL Declare a global variable.
VAR_EXTERNAL Declare a reference to a global variable declared in another file.

You use the END_VAR keyword to indicate the end of a variable block.

Storage Modifiers

You can use storage modifiers after the variable declaration keyword:

Storage ModifierUsed WithDescription
CONSTANTVARDeclares a constant variable locally in a Program Unit.
CONSTANTVAR_GLOBALDeclares a constant variable globally in a file.
CONSTANTVAR_EXTERNALDeclares a constant reference to a global variable. The global variable may or may not be constant.

Access Modifiers

You can use access modifiers after the variable declaration keyword:

Access ModifierDescription
PUBLICCode outside of the Class Declaration can access the variable.
PRIVATEOnly code in the Class Declaration can access the variable.
PROTECTEDReserved for future use.
INTERNALReserved for future use.

Variable Declaration Syntax

Inside a variable declaration block, you declare variables using this syntax:

variable-name [, variable-name ] : variable-type [ := initial-value ];

variable-name ::= name [ AT location ]

Where:

Directly Located Variables

A directly located variable uses the AT location syntax to associate a variable name with a Tag Reference. For best results use a variable-type that is compatible with the type of the Tag Reference.

Examples

  1. Declaration of global variables:
    Var_Global
        X, Y, Z : LReal; (* Coordinates *)
    End_Var
  2. Declaration of global constant value:
    Var_Global Constant
        MaxTags : Int := 12;
    End_Var
  3. Input variable to a program unit:
    Var_Input
        V1, V2 : Vector;
    End_Var

    This example assumes Vector is a previously declared derived data type, for example:

    Type
        Vector : Struct X, Y : REAL; End_Struct;
    End_Type
  4. Temporary array:
    Var_Temp
        Buffer : Array[0..5] Of Int;
    End_Var
  5. Directly located variable:
    Var
        Pressure AT %".TankPressure" : LREAL
    End_Var

Further Information

Elementary Data Type

For the elementary types you can use for variable-type.

Derived Data Type

For the derived types you can use for variable-type.

Glossary

For the meaning of terms used in Fernhill SCADA.