Pascal - 变量作用域



在任何编程语言中,作用域是指程序中一个定义的变量可以存在并且可以访问的区域,超出该区域则无法访问该变量。在 Pascal 编程语言中,可以在三个地方声明变量:

  • 在子程序或块内,称为局部变量

  • 在所有子程序之外,称为全局变量

  • 在子程序参数的定义中,称为形式参数

让我们解释一下什么是局部变量和全局变量以及形式参数。

局部变量

在子程序或代码块内部声明的变量称为局部变量。只有在该子程序或代码块内部的语句才能使用它们。局部变量对外部的子程序是未知的。以下是用局部变量的示例。这里,所有变量abc都是名为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 的过程,该过程将拥有自己的变量集abc,并从程序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 以及它自己的局部变量来说是全局变量。程序可以为局部变量和全局变量使用相同的名称,但函数内部局部变量的值将优先。

让我们稍微修改一下前面的例子,现在过程 display 的局部变量与abc具有相同的名称:

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
广告

© . All rights reserved.