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