Java Reference
In-Depth Information
1. The name of the method
2. The number of parameters, if any
3. The data type of each parameter
4. The data type of the value computed (that is, the value returned) by the
method, called the type of the method
Typically, the value returned by a value-returning method is unique. So it is natural for
you to use the value in one of three ways:
￿ Save the value for further calculation.
￿ Use the value in some calculation.
￿ Print the value.
This suggests that a value-returning method is used in either an assignment statement or
an output statement. That is, a value-returning method is used in an expression.
In addition to the four properties just described, one more thing is associated with
methods (both value-returning and void):
5. The code required to accomplish the task
Before we look at the syntax of a user-defined value-returning method, let's review the
points associated with such methods. The first four properties become part of what is called
the heading of the method; the fifth property (the code) is called the body of the method.
Together, these five properties form what is called the definition of the method.
For predefined methods, you only need to be concerned with the first four properties.
Software companies typically do not give out the actual source code, which is the body of
the method.
For example, for the method abs (absolute), the heading might look like:
public static int abs( int number)
Similarly, the method abs might have the following definition:
public static int abs( int number)
{
if (number < 0)
number = -number;
return number;
}
The variable declared in the heading, within the parentheses, of the method abs is called
the formal parameter of the method abs . Thus, the formal parameter of abs is number .
The program in Example 7-1 contains several statements that use the method pow . In Java
terminology, we say that the method pow is called several times. Later in this chapter, we
discuss what happens when a method is called.
 
Search WWH ::




Custom Search