Variable Declaration

Introduction

You can declare variables locally in a Program Unit or globally in a file. Variables declared locally in a program unit can only be accessed by that Program Unit. Variables declared globally in a file can be accessed by any Program Unit in the file.

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

KeywordUsage
VARDeclare local variables in a Program Unit.
VAR_INPUTDeclare input variables to a Program Unit.
VAR_OUTPUTDeclare output variables to a Program Unit.
VAR_IN_OUTDeclare variable passed by reference to a Program Unit.
VAR_TEMPDeclare a temporary variable in a Program Unit.
VAR_GLOBALDeclare a global variable.
VAR_EXTERNALDeclare 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.

Variable Declaration Syntax

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

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

Where:

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

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.