目录

Pascal - 变量作用域

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

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

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

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

让我们解释什么是localglobal变量和形式参数。

局部变量 (Local Variables)

在子程序或块内声明的变量称为局部变量。 它们只能由子程序或代码块中的语句使用。 本地变量不属于他们自己的子程序。 以下是使用局部变量的示例。 这里,所有变量abc都是名为exLocal程序的本地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

全局变量 (Global Variables)

全局变量在函数之外定义,通常在程序之上。 全局变量将在程序的整个生命周期中保持其值,并且可以在为程序定义的任何函数内访问它们。

任何函数都可以访问global变量。 也就是说,全局变量在声明后可用于整个程序。 以下是使用global变量和local变量的示例 -

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

请注意,过程显示可以访问变量a,b和c,它们是关于显示的全局变量以及它自己的局部变量。 程序对于局部变量和全局变量可以具有相同的名称,但函数内的局部变量值将优先考虑。

让我们稍微改变前面的例子,现在程序显示的局部变量与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
↑回到顶部↑
WIKI教程 @2018