Difference between revisions of "Basic Pascal Tutorial/Chapter 4/Scope/ja"

From Free Pascal wiki
Jump to navigationJump to search
Line 32: Line 32:
 
  20
 
  20
  
The reason is: if two variable with the same identifiers are declared in a subprogram and the main program, the main program sees its own, and the subprogram sees its own (not the main's). The most local definition is used when one identifier is defined twice in different places.
+
こうなるわけは、もし同じ識別子の2つの変数がそれぞれサブプログラムとメインプログラムで宣言されていると、The reason is: if two variable with the same identifiers are declared in a subprogram and the main program, the main program sees its own, and the subprogram sees its own (not the main's). The most local definition is used when one identifier is defined twice in different places.
  
 
Here's a scope chart which basically amounts to an indented copy of a program with just the variables and minus the logic. By stripping away the application logic and enclosing blocks in boxes, it is sometimes easier to see where certain variables are available.
 
Here's a scope chart which basically amounts to an indented copy of a program with just the variables and minus the logic. By stripping away the application logic and enclosing blocks in boxes, it is sometimes easier to see where certain variables are available.

Revision as of 08:25, 24 August 2015

Template:Scope/ja

4D - Scope (著者: Tao Yue, 状態: 原文のまま修正なし)

スコープ(Scope) とはある変数が見える範囲のことを言う。内部に手続きがある手続きや手続きの内部の変数を扱い、各変数がいつその手続きによって見えるのかを知ることが課題だとしよう。

グローバル変数(global variable)はメインのプログラムで定義される変数である。どのサブ・プログラムからでも、それを見ることができ、利用することができ、修正することもできる。サブ・プログラムは自らを呼び出すこともできるし、その前に定義されている他の全てのサブ・プログラムを呼び出すことができる。

ここでのポイントは、コードのどんなブロック内でも(手続き、関数、あるいはその他のなんであれ)、見ることができる識別子(identifiers)は、そのブロック以前に定義されたものか、あるいはそのブロック外にあるものだということである。

program ScopeDemo;
var A : integer;

  procedure ScopeInner;
  var A : integer;
  begin
    A := 10;
    writeln (A)
  end;

begin (* Main *)
  A := 20;
  writeln (A);
  ScopeInner;
  writeln (A);
end. (* Main *)

上のプログラムの出力は以下のようになる。

20
10
20

こうなるわけは、もし同じ識別子の2つの変数がそれぞれサブプログラムとメインプログラムで宣言されていると、The reason is: if two variable with the same identifiers are declared in a subprogram and the main program, the main program sees its own, and the subprogram sees its own (not the main's). The most local definition is used when one identifier is defined twice in different places.

Here's a scope chart which basically amounts to an indented copy of a program with just the variables and minus the logic. By stripping away the application logic and enclosing blocks in boxes, it is sometimes easier to see where certain variables are available.

Scope.gif
  • Everybody can see global variables A, B, and C.
  • However, in procedure Alpha the global definition of A is replaced by the local definition.
  • Beta1 and Beta2 can see variables VCR, Betamax, and cassette.
  • Beta1 cannot see variable FailureToo, and Beta2 cannot see Failure.
  • No subprogram except Alpha can access F and G.
  • Procedure Beta can call Alpha and Beta.
  • Function Beta2 can call any subprogram, including itself (the main program is not a subprogram).
previous contents next