Compiling without the, Option, Sharing public variables – HP SunSoft Pascal 4.0 User Manual

Page 101

Advertising
background image

Separate Compilation

77

5

Compiling without the

-xl

Option

There are three ways of sharing variables and routines across units when you
compile your program without the

–xl

option.

Sharing Public Variables

If you declare a variable in two or more separate units and the variable is

public

in both places, that variable is shared between units. Variables are

public

by default, unless you compile with the

-xl

option, in which case

variables are

private

by default. In this example, the variable

global

is

public

by default, and thus shared between the program and the module.

The program unit,

shrvar_prog.p

program shrvar_prog;

var

global: integer;

procedure proc; external;

begin { program body }

global := 1;

writeln('From MAIN, before PROC: ', global);

proc;

writeln('From MAIN, after PROC: ', global)

end. { shrvar_prog }

The module unit,

shrvar_mod.p

.

The assignment of a new value to

global

and

max_array

in the

procedure

proc

in

shrvar_prog.p

is repeated in

shrvar_mod.p

.

module shrvar_mod;

var

global: integer;

procedure proc;

begin

writeln('From PROC: ',global);

global := global + 1

end; { proc }

Advertising