Java Reference
In-Depth Information
Chapter 13
Using Methods
The Method Call
You transfer control to a method by referencing the method's name in a pro-
gram statement. The method call can appear at any point in the code from
which the method is visible. Furthermore, a method can call itself. A
method that calls itself is said to use recursion.
The following code shows a call to the method named getAverage(),
discussed previously:
public static void main(String[] args)
{
// Local variables
int[] values = {1, 2, 3, 4, 5, 6, 7, 12};
int average;
// Call the method named getAverage()
average = getAverage(values);
// Display results
System.out.println("Average is: " + average);
...
The method call is contained in the statement:
average = getAverage(values);
The call to getAverage() contains the name of the array passed as an ar-
gument. This argument becomes the only parameter in getAverage().
Search WWH ::




Custom Search