プリンキピア

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

Functions関数

Functions are sometimes referred to as, or replaced by, subroutines, subprograms, procedures, or methods. Some computer scientists consider them to be flow of control constructs because calling upon a function to do a job pulls the execution out of the regular sequence. In general, function call is code which invokes the function as well as passes to it values that may be needed for it to do its job while being designed to return values back to the place at which the function was called. However, in some programming languages such as C, functions can only return a value of a given type or no value. In that case, function parameters may be passed as references (i.e., by passing a pointer as a parameter) and the values associated with these parameters may be changed by the function.
英語のヒント
関数は、サブルーチン、サブプログラム、手続き、メソッドと呼ばれたり、それらに置き換えられたりすることがある。ある仕事のために関数を呼び出すと、通常の実行順序から外れるので、制御フローの構造と考えるコンピューター科学者もいる。一般に、関数呼び出しは、関数を起動するとともに、仕事に必要な値を渡すコードである。また、関数を呼んだ場所へ値を返すよう設計される。ただし、Cなどの一部の言語では、関数が返せるのは、所定の型の一つの値、または値なしだけである。その場合、関数の引数を参照として渡せる。つまり、ポインターを引数として渡すことで、関数がそれらの引数に対応する値を変更できる。
A JavaScript alert is an API function that is designed to send a message to a web page and wait for the user to acknowledge it via an OK button, for example. The following illustrates code that invokes such an alert function:
英語のヒント
JavaScriptのalertは、ウェブページにメッセージを送り、たとえばOKボタンを通じて利用者が確認するのを待つよう設計されたAPI関数である。次のコードは、このalert関数を呼び出す。
alert("This message is for you, click OK to continue");
Modularityモジュール性
This refers to the need for a program to be broken into various tasks. An employee program would probably be carrying out many tasks, one of which might be to compute the weekly salary. Modular design allows us to specify these tasks, name them, and call upon them without the finished code. We can define the tasks as functions and build the stub of them without all the details. Then we can call them from our main flow of control and have them respond to the call.
英語のヒント
これは、プログラムをさまざまなタスクに分ける必要性を指す。従業員を扱うプログラムは、おそらく多くのタスクを行い、その一つが週給の計算になるだろう。モジュール設計では、コードが完成する前でも、タスクを定め、名前を付け、呼び出せる。タスクを関数として定義し、細部をすべて実装せずに、その仮の骨組みであるスタブを作れる。そして、メインの制御の流れから呼び出し、呼び出しへ応答させられる。
Maintainability保守性
If we had to change the way we pay our employees, perhaps due to a change in deductions, we have to change that code in a multitude of places. By using a function to carry out the job for all employees, changes can be limited to just one place in the code.
英語のヒント
給与控除の変更などで、従業員への支払い方を変える必要が生じた場合、そのコードを非常に多くの場所で変えなければならない。すべての従業員に対する仕事を、一つの関数で行えば、変更をコード内の一か所だけに限れる。
Reusability再利用性
Building functions to carry out tasks means that we can either reuse the code in other software and/or call upon the same code from other locations. An API that is shipped with most languages is a perfect example. Our code calling upon the PHP print routine shows the efficacy of this—it is not a part of the language itself; rather, it is part of the API that is shipped with it.
英語のヒント
仕事を行う関数を作れば、そのコードを別のソフトウェアで再利用したり、別の場所から同じコードを呼び出したりできる。多くの言語に付属するAPIは、その好例である。PHPのprintルーチンを呼ぶコードは、この有効性を示す。printは言語自体の一部ではなく、それに付属するAPIの一部である。
Function Signature関数シグネチャー
To build a function in an HLL, one must define its function signature. The signature defines a function and informs the compiler or interpreter of details that it needs in order to call upon that function to execute. It also defines what it may return to the code which called the function. The following illustrates a function signature in Java (in this case, the function may be a method) for the task of paying an employee:
英語のヒント
HLLで関数を作るには、その関数シグネチャーを定義する必要がある。シグネチャーは関数を定義し、その関数を呼び出して実行するために必要な詳細を、コンパイラーやインタープリターへ知らせる。また、関数を呼んだコードへ何を返せるかも定める。次は、従業員へ給与を支払う仕事の、Javaの関数シグネチャーの例である。この場合の関数は、メソッドである場合がある。
public double payEmp(String empName, double empBasePay, double empHours);
The function can be called upon to do its job with the following code:
次のコードで、その関数を呼び出して仕事を行わせられる。
myPay = payEmp("John Doe", 15.0,40.0);
The pay amount would be computed by the function and the resulting value would be returned to the point of the call and placed in the variable.
関数が給与額を計算し、その結果を呼び出した地点へ返して、変数に入れる。
The word public is a keyword in Java that is called an access modifier; it denotes where in code this method may be accessed from. In this case, it is available from anywhere in the program code.
英語のヒント
publicは、Javaでアクセス修飾子と呼ばれるキーワードである。コードのどこからこのメソッドへアクセスできるかを示す。この場合、プログラムコード内のどこからでも利用できる。
The keyword double indicates that this method will return a value of that data type to the place where the method was called. In this case, we want it to return the total pay for the employee. In Java, if we use the keyword void for the return, it tells the compiler that we will not be returning any value.
英語のヒント
doubleキーワードは、このメソッドが、そのデータ型の値を呼び出し元へ返すことを示す。この例では、従業員の給与総額を返したい。Javaで戻り値にvoidキーワードを使うと、値を何も返さないことを、コンパイラーへ伝える。
The identifier payEmp is the name we have given to the method. Its syntax follows the same rules and best practices that are used for a variable identifier in this language.
識別子payEmpは、このメソッドに付けた名前である。その構文は、この言語の変数識別子に使う規則や推奨される方法と同じものに従う。
The parentheses indicate that this is a function (method), thereby delineating it from a variable identifier.
英語のヒント
括弧は、これが関数、つまりメソッドであることを示し、変数の識別子と区別する。
A formal parameter represents values that the function needs to receive to do its job. Parameters are not required, but if they are specified, the call of the function must pass actual parameters values for them into the function. Not doing so would result in a compile error which will not allow the program to run. In fact, not complying with any of the signature items results in a compile error.
英語のヒント
仮引数は、仕事を行うために関数が受け取る必要のある値を表す。引数は必須ではないが、指定されていれば、関数呼び出しで、それに対応する実引数の値を渡さなければならない。そうしなければ、コンパイルエラーとなり、プログラムを実行できない。実際、シグネチャーのどの項目に従わなくても、コンパイルエラーになる。