プリンキピア

第7章高水準プログラミング言語

Function Call関数呼び出し
A function call in an HLL must comply with the function signature. A Java call to the function looks like this:
英語のヒント
高水準言語での関数呼び出しは、関数シグネチャーに従わなければならない。Javaでこの関数を呼び出すと、次のようになる。
double thisSalary = payEmp(thisName, thisPayRate, thisHrs);
Starting from left to right, we declare a variable thisSalary and assign to it the value that will be the return of the function. We then call the function, passing the values that are required for its defined parameters. The term argument is used for the values of a function call which must match the parameters in both number and data type.
英語のヒント
左から右へ見ると、まず変数thisSalaryを宣言し、関数の戻り値になる値をその変数に代入する。次に、定義された仮引数に必要な値を渡して関数を呼び出す。関数呼び出しで渡す値には、実引数(argument)という用語を使う。実引数は、数とデータ型の両方で仮引数に一致しなければならない。
Parameter Passing
引数の受け渡し
Parameters are passed using one of two methods. In pass by value, a copy is made of the value and the copy is passed as an argument to the parameter. When it encounters the parameter, the value is assigned and the values in the original variable cannot be affected by any changes which may occur inside the function.
英語のヒント
引数の受け渡しには、二つの方法のいずれかを使う。値渡しでは、値のコピーを作り、それを実引数として仮引数へ渡す。仮引数に到達するとその値が代入され、元の変数に入っている値は、関数内で生じる変更の影響を受けない。
On the other hand, pass by reference is employed in Java to pass a complex data type, meaning that the value stored inside the variable is a pointer to the memory address of the actual complex object. The value copied into the parameter in this case gives the code access to the object itself within the scope of the function so any changes made to its value will be reflected in the actual object that was passed to the function as shown in Figure 7.16.
英語のヒント
これに対し、Javaでは複合データ型を渡す際に参照渡しを使う。これは、変数に保存されている値が、実際の複合オブジェクトのメモリアドレスを指すポインターであることを意味する。この場合、仮引数へコピーされた値によって、関数のスコープ内のコードからオブジェクトそのものにアクセスできる。そのため、その値に加えた変更は、図7.16に示すように、関数へ渡された実際のオブジェクトに反映される。
Figure 7.16

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 7.16 Compare passing a parameter by value versus passing it by reference in C++. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図7.16 C++での、引数の値渡しと参照渡しを比較する。(出典:著作権 Rice University、OpenStax、CC BY 4.0ライセンス)
Let’s code the entire function in Java to get a complete view of its components:
英語のヒント
構成要素の全体像をつかむために、Javaで関数全体を書いてみよう。
public double payEmp(String empName, double empBasePay, double empHours) {
    double empPay = empBasePay * empHours;
    System.out.println(empName + " is being paid " + empPay);
    return empPay;
}
In the example, empBasePay and empHours are passed by value of primitive data types. On the other hand, empName is passed by reference because string is a complex data type.
英語のヒント
この例では、empBasePayとempHoursは基本データ型の値渡しで渡される。これに対し、empNameは、文字列が複合データ型であるため参照渡しで渡される。
Call Stack
呼び出しスタック
The call stack (execution stack) is a data structure that controls the calling and return of functions. Other duties include storing local variables, which dictates scope, defined as the visibility and lifetime of variables. It also has control of parameter passing.
英語のヒント
呼び出しスタック(実行スタック)は、関数の呼び出しと復帰を制御するデータ構造である。ほかにも局所変数を保存する役割があり、それによってスコープ、すなわち変数の可視性と生存期間が決まる。引数の受け渡しも制御する。
Describing the call stack is usually done by comparing it to a cafeteria tray unit. Clean trays are pushed onto the unit as they come in and are popped off the unit as required. This concept is known as last in, first out (LIFO). Thus, when a subroutine is called, it is pushed onto the call stack. Its presence and all of its parts on the stack are called a stack frame. When it finishes executing, it is popped off. The flow of control of the program follows the current state of the call stack. A call stack is outlined in Figure 7.17.
英語のヒント
呼び出しスタックは、よく食堂のトレー収納台にたとえて説明される。洗い終わったトレーは、運ばれてくると台に積まれ、必要に応じて上から取り出される。この考え方を、後入れ先出し(LIFO)という。したがって、サブルーチンが呼び出されると、呼び出しスタックにプッシュされる。スタック上に置かれたそのサブルーチンと、それに属するすべての部分をスタックフレームという。実行が終わると、スタックからポップされる。プログラムの制御フローは、呼び出しスタックの現在の状態に従う。図7.17に呼び出しスタックの概要を示す。
Figure 7.17

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 7.17 This is representative of a call stack. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図7.17 呼び出しスタックの例。(出典:著作権 Rice University、OpenStax、CC BY 4.0ライセンス)
In this implementation, function1 is called first. It is pushed onto the stack in stack frame format and the parameters with their values are pushed first followed by the return address so that the flow of control can revert to the location of the function call after the stack frame has been popped off the stack. Lastly, the local variables are pushed onto the stack frame as they are defined. In this example, function1 has called function2 and has been pushed onto the stack. When it is popped off the stack, control will revert to the function1 stack frame.
英語のヒント
この実装では、最初にfunction1が呼び出される。これはスタックフレームの形でスタックへプッシュされる。まず引数とその値がプッシュされ、次いで戻り先アドレスが置かれる。これにより、スタックフレームがスタックからポップされた後で、制御フローを関数の呼び出し元の位置へ戻せる。最後に、局所変数が定義されるたびに、スタックフレームへプッシュされる。この例では、function1がfunction2を呼び出しており、スタックへプッシュされている。これがスタックからポップされると、制御はfunction1のスタックフレームへ戻る。
Looking at function1, we see that the parameters come into existence when the function is pushed onto the stack and disappear when the function is popped from the stack. The same is true of any local variables declared in the function. This is an illustration of the scope of a variable which controls both the visibility and lifetime of a variable (i.e., a variable that is local to a function is only visible within the scope of that function for the duration of execution of that function). This scope is local because the visibility and lifetime is local to the function; they do not exist and cannot be seen from anywhere else in the program.
英語のヒント
function1を見ると、引数は関数がスタックにプッシュされたときに生成され、スタックからポップされたときに消えることが分かる。関数内で宣言された局所変数も同じである。これは、変数の可視性と生存期間の両方を制御するスコープの例である。つまり、ある関数の局所変数は、その関数の実行中に、その関数のスコープ内でしか参照できない。可視性と生存期間が関数の内部に限られるので、このスコープを局所的という。これらの変数は、プログラムのほかのどの場所にも存在せず、そこから参照することもできない。