Sharing variables between units – HP SunSoft Pascal 4.0 User Manual

Page 95

Advertising
background image

Program Construction and Management

71

4

In a real program,

header.h

would probably contain many declarations and

would be included in several modules. Aside from routine declarations,
header files often contain constant, type, and variable declarations.

Sharing Variables Between Units

Variables that are global across a unit (that is, not declared locally in a routine)
can be

public

or

private

. A

public

variable can be shared by any unit that

is linked to the unit that declares the variable. A

private

variable cannot be

shared.

You can use the

public

and

private

reserved words to declare that a

var

section declares

public

or

private

variables. For example:

When you do not use

public

or

private

, variables are

public

by default.

However, when you compile with the

-xl

option, variables are

private

by

default.

To share a

public

variable, simply declare it in each unit where you want to

share it. As long as the variable is

public

, each reference to that variable

accesses the same data.

program program_unit3 (output);

public var

x : integer;

private var

y : integer;

Advertising