Basic Pascal Tutorial/Chapter 4/Scope/ja
Sent here from Scope/ja.
English · Français · Български · 中文(中国) · 日本語
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回宣言されると最もローカルな定義が使われる。
ここにはスコープの図を示した。基本的に変数だけがあってロジックを除いたプログラムをインデントしたものである。アプリケーションのロジックを取り去り、ブロックを四角で囲うことで、ある変数が利用できる範囲を知ることが簡単になることが多い。 
- 誰もがグローバル変数
A、B、Cをみることができる。 - しかしながら、手続き
Alphaの内部では、Aのグローバルな定義はローカルな定義に置き換えられる。 Beta1とBeta2は変数VCR、Betamax、cassetteをみることができる。Beta1は変数FailureTooをみることができず、Beta2はFailureがみえない。Alpha以外のどのサブプログラムもFとGにアクセスできない。- 手続き
BetaはAlphaとBetaを呼び出すことができる。 - 関数
Beta2はどんなサブプログラムも呼び出せ、それには自分自身も含まれる(メインプログラムはサブプログラムではない)。
| previous | contents | next |
From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 4/Scope/ja", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.
Edited on wiki.freepascal.org by 4 people, 2015–2022
From the official wiki's backup of 26 May 2024: who edited this page there, and when. Edits made there after that date were not recorded, and the earlier versions are not shown here; the main page says why.
| When | Who | What changed |
|---|---|---|
| Derakun | Created page | |
| Derakun | ||
| Derakun | ||
| Derakun | ||
| Derakun | ||
| Derakun | ||
| Derakun | ||
| Derakun | ||
| Derakun | ||
| FTurtle | ||
| Trev | Fixed syntax highlighting (minor) | |
| Kai Burghardt | Kai Burghardt moved page Scope/ja to Basic Pascal Tutorial/Chapter 4/Scope/ja: tidy up main name space: create subpage hierarchy for basic Pascal tutorial [cf. [[Special: PermaLink/149778#Cluttering o... (minor) | |
| Kai Burghardt | bypass redirects [cf. discussion] | |
| Kai Burghardt | bypass [previously missed] redirect [cf. discussion] | |
| Kai Burghardt | bypass language bar/categorization template redirect [cf. discussion] (minor) |
Talk about this page
Nobody has opened a bench for this page yet. Sign in to open one.
