Java Reference
In-Depth Information
Chapter 12
Program Building Blocks
The Java Subprogram
It did not take long for programmers to discover that code often contained
sections that performed identical operations. For example, a program that
performed geometrical calculations had to calculate the area of a circle
over and over again. Recoding the same routine wasted programming ef-
fort, made the program larger, and increased the possibility of error. The so-
lution was to create subprograms within the main program. The
subprogram would contain a processing routine that could be reused as of-
ten as necessary without having to recode it. An added advantage was that
subprograms reduced code size and simplified testing and error correction.
In this case, the subprogram to calculate the area of a circle would receive
the radius parameter from the caller. The subroutine would then perform
the required calculations and return the circle's area.
Java subprograms are called methods. Methods are the building block
of a Java application. This chapter is about methods and how they work.
Modules and Methods
Modern-day programming techniques are based on program modules . The
program breaks the processing task into small units, called modules. The
result is a program that is easier to manage and maintain. The processing
engines within each module are called methods. In Java, a method is a sub-
routine designed to perform a specific and well-defined set of related pro-
cessing operations. Each method has a single entry point and a single exit.
A well conceived method rarely exceeds a few pages of code. It is better to
divide processing into several simpler functions than to create a single
more complex one.
Search WWH ::




Custom Search