Class: Returning a Value from a Method

public Bicycle seeWhosFastest(Bicycle myBike, Bicycle yourBike,
                              Environment env) {
    Bicycle fastest;
    // code to calculate which bike is 
    // faster, given each bike's gear 
    // and cadence and given the 
    // environment (terrain and wind)
    return fastest;
}
  • A method returns to the code that invoked it when it completes all the statements in the method, reaches a return statement, or throws an exception, whichever occurs first.
  • You declare a method's return type in its method declaration. Within the body of the method, you use the returnstatement to return the value.
  • Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so. In such a case, a return statement can be used to branch out of a control flow block and exit the method.
  • The data type of the return value must match the method's declared return type.

Related concepts

Returning a Value from a Method

Class: Returning a Value from a Method — Structure map

Clickable & Draggable!

Class: Returning a Value from a Method — Related pages: