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

From Free Pascal wiki
Jump to navigationJump to search
m (bypass language bar/categorization template redirect [cf. discussion])
 
(11 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Scope/ja}}
+
{{Basic Pascal Tutorial/Chapter 4/Scope}}
  
 
4D - Scope (著者: Tao Yue, 状態: 原文のまま修正なし)
 
4D - Scope (著者: Tao Yue, 状態: 原文のまま修正なし)
  
''スコープ(Scope)'' とはある変数が見える範囲のことを言う。内部に手続きがある手続きや手続きの内部の変数を扱い、各変数がいつその手続きによって見えるのかを知ることが仕事だとしよう。
+
''スコープ(Scope)'' とはある変数が見える範囲のことを言う。内部に手続きがある手続きや手続きの内部の変数を扱い、各変数がいつその手続きによって見えるのかを知ることが課題だとしよう。
  
A global variable is a variable defined in the main program. Any subprogram can see it, use it, and modify it. All subprograms can call themselves, and can call all other subprograms defined before it.
+
グローバル変数(global variable)はメインのプログラムで定義される変数である。どのサブ・プログラムからでも、それを見ることができ、利用することができ、修正することもできる。サブ・プログラムは自らを呼び出すこともできるし、その前に定義されている他の全てのサブ・プログラムを呼び出すことができる。
  
The main point here is: within any block of code (procedure, function, whatever), the only identifiers that are visible are those defined before that block and either in or outside of that block.
+
ここでのポイントは、コードのどんなブロック内でも(手続き、関数、あるいはその他のなんであれ)、見ることができる識別子(identifiers)は、そのブロック以前に定義されたものか、あるいはそのブロック外にあるものだということである。
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
program ScopeDemo;
 
program ScopeDemo;
 
var A : integer;
 
var A : integer;
Line 27: Line 28:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The output of the above program is:
+
上のプログラムの出力は以下のようになる。
 
  20
 
  20
 
  10
 
  10
 
  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.
+
こうなるわけは、もし同じ識別子(identifiers)の2つの変数がそれぞれサブプログラムとメインプログラムで宣言されていると、メインプログラムは自分自身の変数をみて、サブプログラムは自分自身の変数(メインプログラムの変数ではなく)をみるからである。1つの識別子が異なる場所で2回宣言されると最もローカルな定義が使われる。
  
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.
+
ここにはスコープの図を示した。基本的に変数だけがあってロジックを除いたプログラムをインデントしたものである。アプリケーションのロジックを取り去り、ブロックを四角で囲うことで、ある変数が利用できる範囲を知ることが簡単になることが多い。
 
[[Image:Scope.gif|centre]]
 
[[Image:Scope.gif|centre]]
  
* Everybody can see global variables <tt>A, B</tt>, and <tt>C</tt>.
+
* 誰もがグローバル変数 <tt>A、B</tt><tt>C</tt> をみることができる。
* However, in procedure <tt>Alpha</tt> the global definition of <tt>A</tt> is replaced by the local definition.
+
* しかしながら、手続き <tt>Alpha</tt> の内部では、<tt>A</tt> のグローバルな定義はローカルな定義に置き換えられる。
* <tt>Beta1</tt> and <tt>Beta2</tt> can see variables <tt>VCR, Betamax</tt>, and <tt>cassette</tt>.
+
* <tt>Beta1</tt> <tt>Beta2</tt> は変数 <tt>VCR、Betamax</tt><tt>cassette</tt>をみることができる。
* <tt>Beta1</tt> cannot see variable <tt>FailureToo</tt>, and <tt>Beta2</tt> cannot see <tt>Failure</tt>.
+
* <tt>Beta1</tt> は変数 <tt>FailureToo</tt> をみることができず、<tt>Beta2</tt> <tt>Failure</tt> がみえない。
* No subprogram except <tt>Alpha</tt> can access <tt>F</tt> and <tt>G</tt>.
+
* <tt>Alpha</tt> 以外のどのサブプログラムも <tt>F</tt> <tt>G</tt> にアクセスできない。
* Procedure <tt>Beta</tt> can call <tt>Alpha</tt> and <tt>Beta</tt>.
+
* 手続き <tt>Beta</tt> <tt>Alpha</tt> <tt>Beta</tt> を呼び出すことができる。
* Function <tt>Beta2</tt> can call any subprogram, including itself (the main program is not a subprogram).
+
* 関数 <tt>Beta2</tt> はどんなサブプログラムも呼び出せ、それには自分自身も含まれる(メインプログラムはサブプログラムではない)。
  
 
{|style=color-backgroud="white" cellspacing="20"
 
{|style=color-backgroud="white" cellspacing="20"
|[[Functions/ja|previous]]   
+
|[[Basic Pascal Tutorial/Chapter 4/Functions/ja|previous]]   
|[[Contents/ja|contents]]  
+
|[[Basic Pascal Tutorial/Contents/ja|contents]]  
|[[Recursion/ja|next]]
+
|[[Basic Pascal Tutorial/Chapter 4/Recursion/ja|next]]
 
|}
 
|}

Latest revision as of 16:20, 20 August 2022

български (bg) English (en) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

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

こうなるわけは、もし同じ識別子(identifiers)の2つの変数がそれぞれサブプログラムとメインプログラムで宣言されていると、メインプログラムは自分自身の変数をみて、サブプログラムは自分自身の変数(メインプログラムの変数ではなく)をみるからである。1つの識別子が異なる場所で2回宣言されると最もローカルな定義が使われる。

ここにはスコープの図を示した。基本的に変数だけがあってロジックを除いたプログラムをインデントしたものである。アプリケーションのロジックを取り去り、ブロックを四角で囲うことで、ある変数が利用できる範囲を知ることが簡単になることが多い。

Scope.gif
  • 誰もがグローバル変数 A、BC をみることができる。
  • しかしながら、手続き Alpha の内部では、A のグローバルな定義はローカルな定義に置き換えられる。
  • Beta1Beta2 は変数 VCR、Betamaxcassetteをみることができる。
  • Beta1 は変数 FailureToo をみることができず、Beta2Failure がみえない。
  • Alpha 以外のどのサブプログラムも FG にアクセスできない。
  • 手続き BetaAlphaBeta を呼び出すことができる。
  • 関数 Beta2 はどんなサブプログラムも呼び出せ、それには自分自身も含まれる(メインプログラムはサブプログラムではない)。
previous contents next