Java Reference
In-Depth Information
Here, the method returns if it is done or if an error occurs. Be careful, however, because
having too many exit points in a method can destructure your code; so avoid using them
casually. A well-designed method has well-defined exit points.
To review: A void method can return in one of two ways—its closing curly brace is
reached, or a return statement is executed.
Returning a Value
Although methods with a return type of void are not rare, most methods will return a value.
In fact, the ability to return a value is one of the most useful features of a method. You have
already seen one example of a return value: when we used the sqrt( ) function to obtain a
square root.
Return values are used for a variety of purposes in programming. In some cases, such as
with sqrt( ) , the return value contains the outcome of some calculation. In other cases, the
return value may simply indicate success or failure. In still others, it may contain a status
code. Whatever the purpose, using method return values is an integral part of Java pro-
gramming.
Methods return a value to the calling routine using this form of return :
return value ;
Here, value is the value returned. This form of return can be used only with methods that
have a non- void return type. Furthermore, a non- void method must return a value by using
this form of return .
You can use a return value to improve the implementation of range( ) . Instead of dis-
playing the range, a better approach is to have range( ) compute the range and return this
value. Among the advantages to this approach is that you can use the value for other calcu-
lations. The following example modifies range( ) to return the range rather than displaying
it.
Search WWH ::




Custom Search