Java Reference
In-Depth Information
Java syntax: Function call (or function invocation)
function-name ( argument , ... , argument )
Each argument is an expression; its type is the same as or narrower than
the type of corresponding parameter of the function being called.
Example : larger(x * x + y, y * y + x)
Purpose : View evaluation of a function call as yielding the value given
by the specification of the function (with the parameters replaced by the
arguments of the call).
The function call
Writing a function call, or function invocation, is similar to writing a procedure
call. The parameter declarations tell us the types of the arguments to use in the
call, and the specification tells us what they are for. We give an example with a
bit of an oddity: x and y are used as parameter names as well as names of the
variables in the arguments. The process does not change: replace the parameter
names with the values of the corresponding arguments.
Recall method larger , whose specification and header we gave earlier:
/** = the larger of x and y */
public int larger( int x, int y) { … }
Suppose we want to find the larger of two expressions:
the larger of x*x+y and y*y+x .
The specification of function larger has the same form as our desired
value; it just has parameters x and y in place of the expressions x*x+y and y
*y+x . Therefore, the desired value will be calculated by the function call
larger(x *x+y, y*y+x)
Note that a function call does not terminate in a semicolon, the way a pro-
cedure call does. A procedure call is a statement to be executed; a function call
is an expression to be evaluated. For example, we could use a function call with-
in another expression:
45 + larger(x *x+y, y*y+x)
Self-review exercises for calls
Below are the specifications of a few methods:
/** Print x , x 2 , and x 3 on a single line */
public static void print3( int x)
Search WWH ::

Custom Search