
Chapter 5. Data Types
73
5.5.2 Local Variables
A local variable can be accessed only in a file where it is defined. Write PRIVATE preceding a
desired variable name or DEFREG statement.
(Example) PRIVATE aaa%
PRIVATE bbb$[10]
PRIVATE ccc$(5,3)[30]
PRIVATE DEFREG ddd
PRIVATE DEFREG eee%(5)
Before access to a local variable, you should define it.
If used inside more than one SUB or FUNCTION function in the same file where the local
variable is defined, all of those variables will also have the same value.
(Example) PRIVATE aa%
FUNCTION addaa(x)
addaa=aa%+x
END FUNCTION
SUB printaa(x)
print aa%+x
END SUB
aa%=2
print addaa(2)
printaa(2)
In the above example, the variable aa% used in "addaa" and "printaa" will have the same
value.
Variables with overlapping scope
Variables with overlapping scopeVariables with overlapping scope
Variables with overlapping scope
If your program has a global variable and a local variable with the same name, in those files
where the variable is declared with the GLOBAL statement, those variables will be treated as
the same; in a file where the variable is declared with the PRIVATE variable, the variable is
available only in that file.
(Example) If in each of Files 1 and 2 the variable aa% is declared by the GLOBAL statement
but in File 3 it is not declared by the GLOBAL statement:
File 1
GLOBAL aa%[50]
File 2
GLOBAL aa%[50]
File 3
PRIVATE aa%[50]
If you link Files 1, 2, and 3 above into a program file, the variables aa% in Files 1 and 2 will
have the same value and aa% in File 3 will be treated as a variable different from those in
Files 1 and 2.
Comentarios a estos manuales