Declaring variables
Previous  Top  Next

The basic syntax for a variable declaration is

var identifierList: type;

where identifierList is a comma-delimited list of valid identifiers and type is any valid type. For example,

var I: Integer;

declares a variable I of type Integer, while

var X, Y: Real;

declares two variables—X and Y—of type Real.
Consecutive variable declarations do not have to repeat the reserved word var:

var

  X, Y, Z: Double;
  I, J, K: Integer;
  Digit: 0
..9;
  Okay: Boolean;


Variables declared within a procedure or function are sometimes called local, while other variables are called global. Global variables can be initialized at the same time they are declared, using the syntax

var identifier: type = constantExpression;

where constantExpression is any constant expression representing a value of type type. Thus the declaration

var I: Integer = 7;

is equivalent to the declaration and statement

var I: Integer;

 ...
I := 7
;

Multiple variable declarations (such as var X, Y, Z: Real;) cannot include initializations, nor can declarations of variant and file-type variables.
If you don't explicitly initialize a global variable, the compiler initializes it to 0. Local variables, in contrast, cannot be initialized in their declarations and contain random data until a value is assigned to them.