Pascal - 变量作用域
任何编程中的作用域都是程序的一个区域,其中已定义的变量可以存在,超出该区域则无法访问该变量。 Pascal 编程语言中可以在三个地方声明变量 −
在子程序或块内称为局部变量
在所有子程序之外称为全局变量
子程序中参数的定义称为形式参数
让我们解释一下什么是局部和全局变量以及形式参数。
局部变量
在子程序或块内声明的变量称为局部变量。 它们只能由该子程序或代码块内的语句使用。 局部变量对其自身之外的子程序来说是未知的。 以下是使用局部变量的示例。 这里,所有变量 a、b 和 c 都是名为 exLocal 的程序的本地变量。
program exLocal;
var
a, b, c: integer;
begin
(* actual initialization *)
a := 10;
b := 20;
c := a + b;
writeln('value of a = ', a , ' b = ', b, ' and c = ', c);
end.
当上面的代码被编译并执行时,会产生以下结果 −
value of a = 10 b = 20 c = 30
现在,让我们进一步扩展该程序,创建一个名为 display 的过程,它将拥有自己的一组变量 a, b 和 c 并直接从程序 exLocal 中显示它们的值。
program exLocal;
var
a, b, c: integer;
procedure display;
var
a, b, c: integer;
begin
(* local variables *)
a := 10;
b := 20;
c := a + b;
writeln('Winthin the procedure display');
writeln('value of a = ', a , ' b = ', b, ' and c = ', c);
end;
begin
a:= 100;
b:= 200;
c:= a + b;
writeln('Winthin the program exlocal');
writeln('value of a = ', a , ' b = ', b, ' and c = ', c);
display();
end.
当上面的代码被编译并执行时,会产生以下结果 −
Within the program exlocal value of a = 100 b = 200 c = 300 Within the procedure display value of a = 10 b = 20 c = 30
全局变量
全局变量在函数外部定义,通常在程序顶部。 全局变量将在程序的整个生命周期中保留其值,并且可以在为程序定义的任何函数内访问它们。
全局变量可以被任何函数访问。 也就是说,全局变量在声明后即可在整个程序中使用。 以下是使用全局和局部变量的示例 −
program exGlobal;
var
a, b, c: integer;
procedure display;
var
x, y, z: integer;
begin
(* local variables *)
x := 10;
y := 20;
z := x + y;
(*global variables *)
a := 30;
b:= 40;
c:= a + b;
writeln('Winthin the procedure display');
writeln(' Displaying the global variables a, b, and c');
writeln('value of a = ', a , ' b = ', b, ' and c = ', c);
writeln('Displaying the local variables x, y, and z');
writeln('value of x = ', x , ' y = ', y, ' and z = ', z);
end;
begin
a:= 100;
b:= 200;
c:= 300;
writeln('Winthin the program exlocal');
writeln('value of a = ', a , ' b = ', b, ' and c = ', c);
display();
end.
当上面的代码被编译并执行时,会产生以下结果 −
Within the program exlocal value of a = 100 b = 200 c = 300 Within the procedure display Displaying the global variables a, b, and c value of a = 30 b = 40 c = 70 Displaying the local variables x, y, and z value of x = 10 y = 20 z = 30
请注意,程序display可以访问变量a、b和c,它们是与display相关的全局变量以及它自己的局部变量。 程序中的局部变量和全局变量可以具有相同的名称,但函数内的局部变量的值优先。
让我们稍微改变一下前面的例子,现在程序显示的局部变量与a,b,c同名 −
program exGlobal;
var
a, b, c: integer;
procedure display;
var
a, b, c: integer;
begin
(* local variables *)
a := 10;
b := 20;
c := a + b;
writeln('Winthin the procedure display');
writeln(' Displaying the global variables a, b, and c');
writeln('value of a = ', a , ' b = ', b, ' and c = ', c);
writeln('Displaying the local variables a, b, and c');
writeln('value of a = ', a , ' b = ', b, ' and c = ', c);
end;
begin
a:= 100;
b:= 200;
c:= 300;
writeln('Winthin the program exlocal');
writeln('value of a = ', a , ' b = ', b, ' and c = ', c);
display();
end.
当上面的代码被编译并执行时,会产生以下结果 −
Within the program exlocal value of a = 100 b = 200 c = 300 Within the procedure display Displaying the global variables a, b, and c value of a = 10 b = 20 c = 30 Displaying the local variables a, b, and c value of a = 10 b = 20 c = 30

