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

From Lazarus wiki
Jump to navigationJump to search
Line 34: Line 34:
 
これは'''値渡し(call-by-value)'''と呼ばれる。これは変数の'''値'''を手続きに渡す。
 
これは'''値渡し(call-by-value)'''と呼ばれる。これは変数の'''値'''を手続きに渡す。
  
パラメータを渡すもう一つの方法は'''参照渡し(call-by-reference)'''と呼ばれる。これは仮パラメータと実パラメータの間に'''リンク'''を作り出す。仮パラメータが手続き内で変更されると、実パラメータも同様に変更される。This creates a '''link''' between the formal parameter and the actual parameter. When the formal parameter is modified in the procedure, the actual parameter is likewise modified. ''Call-by-reference'' is activated by preceding the parameter group with a <tt>VAR</tt>:
+
パラメータを渡すもう一つの方法は'''参照渡し(call-by-reference)'''と呼ばれる。これは仮パラメータと実パラメータの間に'''リンク'''を作り出す。仮パラメータが手続き内で変更されると、実パラメータも同様に変更される。'''参照渡し'''はパラメータ・グループの前に <tt>VAR</tt> をつけると利用できる。
 
<syntaxhighlight>
 
<syntaxhighlight>
VAR identifier1, identifier2, ..., identifiern : datatype;
+
VAR 識別子1, 識別子2, ..., 識別子n : データ型;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
In this case, constants and literals are not allowed to be used as actual parameters because they might be changed in the procedure.
+
この場合、定数と値そのもの(literals)は実パラメータとして利用できない。それらは手続き内で変化させることができないからである。
  
Here's an example which mixes ''call-by-value'' and ''call-by-reference'':
+
ここでは''値渡し''''参照渡し''を混ぜた例を示そう。
 
<syntaxhighlight>
 
<syntaxhighlight>
 
procedure Name (a, b : integer; VAR c, d : integer);
 
procedure Name (a, b : integer; VAR c, d : integer);
Line 57: Line 57:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Immediately after the procedure has been run, gamma has the value 3 because c was a reference parameter, but alpha still is 1 because a was a value parameter.
+
手続きが開始されるとすぐに gamma 3 という値をとる。なぜなら c は参照パラメータだからである。しかし、 alpha はまだ 1 のままである。 a は値パラメータだからである。
  
This is a bit confusing. Think of call-by-value as copying a variable, then giving the copy to the procedure. The procedure works on the copy and discards it when it is done. The original variable is unchanged.
+
これはちょっと混乱を招くかもしれない。値参照は変数をコピーし、それからそのコピーを手続きに与えるものと考えるとよい。手続きはそのコピーで仕事をして、終わるとそれを捨てる。オリジナルは変わらないままである。
  
 
Call-by-reference is giving the actual variable to the procedure. The procedure works directly on the variable and returns it to the main program.
 
Call-by-reference is giving the actual variable to the procedure. The procedure works directly on the variable and returns it to the main program.

Revision as of 09:04, 19 August 2015

Template:Parameters/ja

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

引数リスト(parameter list)は手続きのヘッディングの部分として含まれる。引数リストは変数値をメインのプログラムから手続きへと引き渡すことを可能にする。新しい手続きのヘッディングは次のようになる。

手続き名 (仮引数リスト);

引数リストが複数の引数グループから成っているときは、セミコロンで区切る。

引数グループ1; 引数グループ2; ... ; 引数グループn

各引数グループは以下の形式をとる。

識別子1, 識別子2, ... , 識別子n : データ型

手続きは仮引数リスト(formal parameter list)と同じ数で同じタイプの実引数(actual argument)を受け渡すことで呼び出される。

procedure Name (a, b : integer; c, d : real);
begin
  a := 10;
  b := 2;
  writeln (a, b, c, d)
end;

上記の手続きをメインのプログラムから、以下のように呼んだとしよう。

alpha := 30;
Name (alpha, 3, 4, 5);

メインのプログラムに戻ったとき、alpha の値は何だろう? 答えは30。だが、alpha は a に渡され、それには10が割り当てられていたはずである。実際には a と alpha は完全に区別される。メインのプログラムでの値は手続きで起きたことには影響されないのである。

これは値渡し(call-by-value)と呼ばれる。これは変数のを手続きに渡す。

パラメータを渡すもう一つの方法は参照渡し(call-by-reference)と呼ばれる。これは仮パラメータと実パラメータの間にリンクを作り出す。仮パラメータが手続き内で変更されると、実パラメータも同様に変更される。参照渡しはパラメータ・グループの前に VAR をつけると利用できる。

VAR 識別子1, 識別子2, ..., 識別子n : データ型;

この場合、定数と値そのもの(literals)は実パラメータとして利用できない。それらは手続き内で変化させることができないからである。

ここでは値渡し参照渡しを混ぜた例を示そう。

procedure Name (a, b : integer; VAR c, d : integer);
begin
  c := 3;
  a := 5
end;

begin
  alpha := 1;
  gamma := 50;
  delta := 30;
  Name (alpha, 2, gamma, delta);
end.

手続きが開始されるとすぐに gamma は 3 という値をとる。なぜなら c は参照パラメータだからである。しかし、 alpha はまだ 1 のままである。 a は値パラメータだからである。

これはちょっと混乱を招くかもしれない。値参照は変数をコピーし、それからそのコピーを手続きに与えるものと考えるとよい。手続きはそのコピーで仕事をして、終わるとそれを捨てる。オリジナルは変わらないままである。

Call-by-reference is giving the actual variable to the procedure. The procedure works directly on the variable and returns it to the main program.

In other words, call-by-value is one-way data transfer: main program to procedure. Call-by-reference goes both ways.

previous contents next