Java Reference
In-Depth Information
if(aValue%2!=0)
return true;
else
return false;
}
The following cases are possible in regards to a method's return:
1. The method can contain one or more explicit return statements. Each state-
ment can be associated with a different value. For example:
return (error_code);
return((r + r) * PI);
return 2 * radius;
2. A method can contain no return statement. In this case, execution con-
cludes at the closing brace and the method is said to “fall of the edge.” A
non-void method that falls off the edge has an undefined return value.
3. A method can end with a simple return statement. This happens when the
method concludes with the expression:
return;
In this case no specific value is returned to the caller. Void methods must
use this style.
4. A method can return a constant, optionally enclosed in parentheses
return 0;
return (1);
Arguments and Parameters
A method's declaration contains a list of the variables, enclosed in
parentheses, whose values are passed by the caller. The method's call state-
ment contains (also enclosed in parentheses) the value passed to the
method as arguments. Suppose that you have coded a method named
getArea() that calculates the area of a triangle from its height and base di-
mensions. Also assume that getArea() receives the height and base values
as parameters, as follows:
double area;
double height = 12.7;
double base = 7.9;
...
// Calling the method
area = getArea(base, height);
...
// Method
public static double getArea(double b, double h)
{
Search WWH ::




Custom Search