Java Reference
In-Depth Information
Java syntax: Procedure call (or procedure invocation)
procedure-name
(
argument
,
...
,
argument
) ;
Each
argument
is an expression whose type is the same as or narrow-
er than the corresponding parameter of the procedure being called.
Example:
drawRect(5, 10, 20, 30);
Purpose: We can view execution of a procedure call as doing what the
specification of the procedure call says (with the parameters in the
specification replaced by the arguments of the call).
/** =
the larger of x and
y */
public int
larger(
int
x,
int
y) { … }
You can tell that this is a function because
of
the type
int
after
public
, which
indicates the type of value that the function produces. The specification indicates
that a call to the function evaluates to the larger of the two parameters
x
and
y
.
2.2.2
The procedure call
We now explain how the method specification and method header are used in
writing a
method call
, or
method invocation
, as it is sometimes called.
Activity
2-1.4
/**
Draw a line from pixel
(x1, y1)
to pixel
(x2, y2)
.
*/
public void
drawLine(
int
x1,
int
y1,
int
x2,
int
y2) {
…
}
Suppose we want to use procedure
drawLine
, shown above, to draw a line
in the graphics window from pixel
(20, 20)
to pixel
(80, 40)
. Notice that if
x1
,
y1
,
x2
, and
y2
in procedure
drawLine
are replaced with
20
,
20
,
80
, and
40
, the
drawLine
spec says that the procedure will do exactly what we want:
Draw a line from pixel
(20, 20)
to pixel
(80, 40)
To write this statement in Java, we use a form of statement called the
pro-
cedure call
. Here is an example:
drawLine(20, 20, 80, 40);
This procedure call consists of:
•
The name of the procedure,
drawLine
.
•
A list of four integers, separated by commas and enclosed in parentheses;
these are the
arguments
of the call.
•
A semicolon.
A method call has one argument for each parameter of the method. The first
argument corresponds to the first parameter, the second argument to the second
Search WWH ::
Custom Search