Pascal-variable-scope

提供:Dev Guides
移動先:案内検索

パスカル-可変範囲

プログラミングのスコープとは、定義された変数が存在し、その変数を超えてアクセスできないプログラムの領域です。 Pascalプログラミング言語で変数を宣言できる場所は3つあります-

  • サブプログラムまたはローカル変数と呼ばれるブロック内

  • グローバル変数と呼ばれるすべてのサブプログラムの外側

  • 仮パラメータと呼ばれるサブプログラムパラメータの定義

    *local* および *global* 変数と仮パラメーターとは何かを説明しましょう。

ローカル変数

サブプログラムまたはブロック内で宣言される変数は、ローカル変数と呼ばれます。 それらは、そのサブプログラムまたはコードブロック内にあるステートメントでのみ使用できます。 ローカル変数は、それ自身の外部のサブプログラムには知られていません。 以下は、ローカル変数を使用した例です。 ここで、すべての変数_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

グローバル変数

グローバル変数は、関数の外側、通常はプログラムの上部で定義されます。 グローバル変数は、プログラムの存続期間を通じてその値を保持し、プログラムに定義された関数のいずれかからアクセスできます。

*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にアクセスできることに注意してください。変数a、b、cは、表示に関してグローバル変数であると同時に、独自のローカル変数です。 プログラムはローカル変数とグローバル変数に同じ名前を持つことができますが、関数内のローカル変数の値が優先されます。

前の例を少し変更してみましょう。手順表示のローカル変数は、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