Variable Declaration [ HP Pascal/iX Reference Manual ] MPE/iX 5.0 Documentation
HP Pascal/iX Reference Manual
Variable Declaration
A variable declaration introduces an identifier as a variable of a
specified type. Each variable is a statically-declared object that
occupies storage and is accessible for the activation and duration of the
program, procedure, or function in which it is declared.
Components of a structured variable may be accessed using an appropriate
selector. Pointer variable dereferencing accesses dynamic variables on
the heap. Module variables are accessible for the duration of the
program that imports the module.
Several identifiers may be combined in the same variable declaration if
the variables are of the same type.
HP Pascal predefines two standard variables, input and output, that are
textfiles. Formally,
VAR
input, output: text;
These standard textfiles commonly appear as program parameters and serve
as default files for various file operations. For more information on
textfiles, refer to Chapter 3 .
Every declaration of a file variable F with components of type T implies
the additional declaration of a buffer variable of type T. The buffer
variable, denoted as F^, may be used to access the current component of
the file F.
Global Variables
Global variables are declared at the beginning of the outermost block of
a program and are available to all the procedures and functions within
that program.
Local Variables
Local variables are variables declared within a particular procedure or
function or in the headings as parameters, and their scope is limited to
that procedure or function during the execution of the procedure or
function. When optimization is requested, the compiler will
issue warnings about local variables that are used prior to their
initialization.
Module Variables
Module variables are declared in either the EXPORT or IMPLEMENT section
of a module. Variables declared in the EXPORT part are available to all
the procedures and functions within the program which imports the
modules. Those declared in the IMPLEMENT section are only available
inside the module.
VAR
This reserved word delimits the beginning of variable declarations in an
HP Pascal program or module. A variable declaration associates an
identifier with a type. The identifier may then appear as a variable in
executable statements.
The reserved word VAR precedes one or more variable declarations. A
variable declaration consists of an identifier, a colon (:), and a type.
Any number of identifiers may be listed, separated by commas. These
identifiers are then variables of the same type.
The type may be any simple, structured, or pointer type. The form of the
type may be a standard identifier, a declared type identifier, or a data
type.
VAR sections may be repeated and intermixed with CONST, TYPE, MODULE, and
IMPORT sections.
Syntax
Variable_decl:
Example
TYPE
answer = (yes, no, maybe);
VAR
pagecount,
linecount,
charcount: integer; { Standard identifier. }
whats_the: answer; { User-declared identifier. }
album : RECORD { Data type. }
speed: (lp, for5, sev8);
price: real;
name : string[20];
END;
Side-Effects
A side-effect is the modification by a procedure or function of a
variable that is global or nonlocal in scope to the procedure or
function. If a local variable is declared using the same identifier as a
global variable, the local variable may be modified without affecting the
global variable.
Example
PROGRAM show_effects(output);
VAR
i,j: integer; { Global variables }
PROCEDURE oops(i : integer); { i is local to the procedure }
BEGIN
IF i > 0 THEN j := j - 1; { j is a global variable }
END;
BEGIN
i := 2;
j := 3;
oops(i);
IF i = j THEN writeln('There was a side effect.');
END.
Output:
There was a side effect.
NOTE Side effect modifications may cause an optimizer to be more
conservative in its choices for code improvement, thereby
decreasing execution performance.
MPE/iX 5.0 Documentation