The Java Course provides a general introduction to programming in Java. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details.


Return Values

Some of the built-in methods we have used, like the Math functions, have produced results. That is, the effect of invoking the method is to generate a new value, which we usually assign to a variable or use as part of an expression. For example:

    double e = Math.exp (1.0);
    double height = radius * Math.sin (angle);

But so far all the methods we have written have been void methods; that is, methods that return no value. When you invoke a void method, it is typically on a line by itself, with no assignment:

    nLines (3);
    g.drawOval (0, 0, width, height);

In this chapter, we are going to write methods that return things, which I will refer to as fruitful methods, for want of a better name. The first example is area, which takes a double as a parameter, and returns the area of a circle with the given radius:

  public static double area (double radius) {
    double area = Math.PI * radius * radius;
    return area;
  }

The first thing you should notice is that the beginning of the method definition is different. Instead of public static void, which indicates a void method, we see public static double, which indicates that the return value from this method will have type double. I still haven't explained what public static means, but be patient.

Also, notice that the last line is an alternate form of the return statement that includes a return value. This statement means, "return immediately from this method and use the following expression as a return value." The expression you provide can be arbitrarily complicated, so we could have written this method more concisely:

  public static double area (double radius) {
    return Math.PI * radius * radius;
  }

On the other hand, temporary variables like area often make debugging easier. In either case, the type of the expression in the return statement must match the return type of the method. In other words, when you declare that the return type is double, you are making a promise that this method will eventually produce a double. If you try to return with no expression, or an expression with the wrong type, the compiler will take you to task.

Sometimes it is useful to have multiple return statements, one in each branch of a conditional:

  public static double absoluteValue (double x) {
    if (x < 0) {
      return -x;
    } else {
      return x;
    }
  }

Since these returns statements are in an alternative conditional, only one will be executed. Although is legal to have more than one return statement in a method, you should keep in mind that as soon as one is executed, the method terminates without executing any subsequent statements.

Code that appears after a return statement, or any place else where it can never be executed, is called dead code. Some compilers warn you if part of your code is dead.

If you put return statements inside a conditional, then you have to guarantee that every possible path through the program hits a return statement. For example:

  public static double absoluteValue (double x) {
    if (x < 0) {
      return -x;
    } else if (x > 0) {
      return x;
    }                          // WRONG!!
  }

This program is not legal because if x happens to be 0, then neither condition will be true and the method will end without hitting a return statement. A typical compiler message would be "return statement required in absoluteValue," which is a confusing message considering that there are already two of them.



Last Update: 2011-01-24