pTAL Reference Manual (H06.08+)
Language Elements
HP pTAL Reference Manual—523746-006
2-11
Scope
Variables that have different scopes can have the same name, but they are different
variables.
For local and sublocal variables, the compiler generates code to evaluate and store an
initialization expression. For example, for the expression
int k := i * j;
if i, j, and k are local or sublocal variables, the compiler generates code to multiply i
by j and store the product in k.
For global variables, the compiler does not generate such initialization code. Initial
values assigned to global variables are determined by the linker.
Example 2-3. Scope of Declared Items
int i; ! i has global scope and is visible everywhere
! from this point forward.
proc p; ! p has global scope. If p had formal
! parameters, they would have local scope.
begin
int j := i; ! j has local scope and is visible everywhere in
! procedure p from this point forward.
subproc s; ! s has local scope. If s had formal parameters,
! they would have sublocal scope.
begin
int k := i + j; ! k has sublocal scope and is visible only
! in subprocedure s.
end;
! k is not visible here. It is created and destroyed every
! time subprocedure s is called.
end;
! j is not visible here. It is created and destroyed every time
! procedure p is called.
! i is accessible here and exists as long as the program is
! running.
Example 2-4. Global and Local Variable With the Same Name
int(32) i; ! This i is global
proc p;
begin
int i; ! This i is local to procedure p, different
! from global variable i, and makes access to
! global variable i impossible ("hides" it).
i := i + 1D; ! ERROR: local variable i is INT(16)
end;










