Java Reference
In-Depth Information
Actual parameters
Method call
num = larger(23.50, 37.80);
Method call
Actual parameters
num = larger(num1, num2);
Actual parameters
Method call
num = larger(34.50, num1);
FIGURE 7-2 Method calls
Note that you can write the method larger as follows:
public static double larger( double x, double y)
{
7
if (x >= y)
return x;
else
return y;
}
Because the execution of a return statement in a method terminates the method, the
preceding definition of the method larger can also be written (without the word else )as:
public static double larger( double x, double y)
{
if (x >= y)
return x;
return y;
}
The return statement can appear anywhere in the method. Recall that once a return
statement executes, all subsequent statements are skipped. Thus, it's a good idea to
return the value as soon as it is computed.
Example 7-2 further shows how to use the method larger .
EXAMPLE 7-2
Now that the method larger is written, the following Java code further illustrates how to use it:
double firstNum = 13;
double secondNum = 36;
double maxNum;
 
Search WWH ::




Custom Search