C++ programs are typically written by combining “prepackaged” functions and classes available in the C++ Standard Library with new functions and classes you write. The C++ Standard Library provides a rich collection of functions for common mathematical calculations, string manipulations, character manipulations, input/output, error checking and many other useful operations.
Functions allow you to modularize a program by separating its tasks into self-contained units. You’ve used a combination of library functions and your own functions in almost every program you’ve written.
There are several motivations for modularizing a program with functions:
Software reuse. For example, in earlier programs, we did not have to define how to read a line of text from the keyboard—C++ provides this capability via the getline
function of the <string>
header.
Avoiding code repetition.
Dividing a program into meaningful functions makes the program easier to test, debug and maintain.
To promote software reusability, every function should be limited to performing a single, well-defined task, and the name of the function should express that task effectively.
As you know, a function is invoked by a function call, and when the called function completes its task, it either returns a result or simply returns control to the caller. An analogy to this program structure is the hierarchical form of management (Figure 6.1).
A boss (similar to the calling function) asks a worker (similar to a called function) to perform a task and report back (i.e., return) the results after completing the task. The boss function does not know how the worker function performs its designated tasks. The worker may also call other worker functions, unbeknownst to the boss. This hiding of implementation details promotes good software engineering. Figure 6.1 shows the boss
function communicating with several worker functions. The boss
function divides the responsibilities among the worker
functions, and worker1
acts as a “boss function” to worker4
and worker5
. The relationship does not need to be hierarchical, but often it is, which makes it easier to test, debug, update and maintain programs.