Quick start manual
Data types, variables, and constants
5-41
Variables
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.
(For more information about constant expressions, see “Constant expressions” on
page 5-44.) 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.
When you declare a variable, you are allocating memory which is freed
automatically when the variable is no longer used. In particular, local variables exist
only until the program exits from the function or procedure in which they are
declared. For more information about variables and memory management, see
Chapter 11, “Memory management”.
Absolute addresses
You can create a new variable that resides at the same address as another variable. To
do so, put the directive absolute after the type name in the declaration of the new
variable, followed by the name of an existing (previously declared) variable. For
example,
var
Str: string[32];
StrLen: Byte absolute Str;
specifies that the variable StrLen should start at the same address as Str. Since the first
byte of a short string contains the string’s length, the value of StrLen is the length of
Str.
You cannot initialize a variable in an absolute declaration or combine absolute with
any other directives.