5.3 Methods that Return Values

Methods can be divided into two groups, often known as commands and queries. The methods that we have been looking at so far are all examples of commands; they are called to perform some task and, once that task is complete, they simply return control to the point at which they were called. Queries, on the other hand, are used to calculate some value which is then returned to the point at which the method was called. In many programming languages, there are two kinds of methods, often known as procedures (to implement commands) and functions (to implement queries). Although Java does not use either of the terms procedure or function, methods of both types can be created. To see how to create queries, let us consider an example.

Example 1

The following method calculates values of the function whose defining equation is


public static double f (double x)
{
   double y;
   if (x < 0)
      y = x * x * x;
   else
      y = x * x;
   return y;
}
There are two new features to note about this method definition.
  1. The word void in the header has been replaced by the word double. This is the type of value that the method will be returning.
  2. The method contains the statement return y; rather than simply having the word return. This indicates, as before, that the method should terminate at this point. In addition, however, it should also send the value of y back to the point at which the method was called.

The return statement need not be at the end of the method and there may be more than one such statement. In Example 1 we could have written the method definition in the form

public static double f (double x)
{
   if (x < 0)
      return x * x * x;
   else
      return x * x;
} 
We can visualize the process of using a query with diagrams like those we used for commands. The next example illustrates a call to the method f, using the version of f shown in Example 1.

Example 2

Suppose that we make a call to the method f by writing
   double a = 3;
   double b = f(a-i);
This would produce the following sequence of events.
  1. The value of the argument of f is evaluated.

  2. This value is then assigned to the parameter x in the method f.

  3. The method f is then executed, assigning a value to y.

  4. When execution of the method is complete, the value 4.0 is returned to the point of the call. The value returned by the method is assigned to b.


If a method returns a value of type <t>, then it can be called in any context in which an expression of type <t> can be used. We have already seen this in calling pre-defined methods like Math.sqrt. The principle also applies to methods that we have written ourselves.

Example 3

Assuming that f has been defined as shown in Example 1 and that all variables shown are of type double, then each of the following calls is valid.
  1. fVal = f (2*z);
  2. total = p + q + fer);
  3. smaller = Math.min(f(s),f(t));
  4. System.out.println("The result is " + f(a+b));

The rules for parameter passing with methods that return values are identical to those for methods that do not do so. The number of arguments must be equal to the number of parameters and the types must be appropriate.

Exercise 5.3

  1. Study this method and then answer the questions that follow it.
    public static int mystery (double a, double b)
    {
       int value = 0;
       if (a < b)
          value = -1;
       if (a > b)
          value = 1;
       return value;
    } 
    1. What is the identifier of the method?
    2. What are its parameters?
    3. What type of value is returned by the method?
    4. What part of the definition forms the heading?
    5. Rewrite the method using a nested if structure.
    6. Rewrite the method using multiple return statements.


  2. The following method definitions lack both punctuation and indentation. Rewrite each definition correcting these defects and state, in a few words, the purpose of each method.
    1. public static char first(char a char b){if(a<b)
      return a else return b}
    2. public static double second(double a double b)
      {double answer if(a<b)answer=a-b else answer=b-a
      return answer}


  3. Assuming that the method f has been defined as it was in Example 1, state, with reasons, which of the following fragments are invalid.
    1. System.out.println(f(-7));
    2. double x = f(-7);
    3. double x = System.out.println(f(-7));
    4. double x = -7; f(x);


  4. Write a method largest that returns the value of the largest of its three double parameters.


  5. Write a method gcd that returns the value of the greatest common divisor of its two int parameters