Main Page

Previous Page
Next Page

[Page 240 (continued)]

6.2. Program Components in C++

C++ programs are typically written by combining new functions and classes the programmer writes with "prepackaged" functions and classes available in the C++ Standard Library. In this chapter, we concentrate on functions.

The C++ Standard Library provides a rich collection of functions for performing common mathematical calculations, string manipulations, character manipulations, input/output, error checking and many other useful operations. This makes the programmer's job easier, because these functions provide many of the capabilities programmers need. The C++ Standard Library functions are provided as part of the C++ programming environment.

Software Engineering Observation 6.1

Read the documentation for your compiler to familiarize yourself with the functions and classes in the C++ Standard Library.

Functions (called methods or procedures in other programming languages) allow the programmer to modularize a program by separating its tasks into self-contained units. You have used functions in every program you have written. These functions are sometimes referred to as user-defined functions or programmer-defined functions. The statements in the function bodies are written only once, are reused from perhaps several locations in a program and are hidden from other functions.

There are several motivations for modularizing a program with functions. One is the divide-and-conquer approach, which makes program development more manageable by constructing programs from small, simple pieces. Another is software reusabilityusing existing functions as building blocks to create new programs. For example, in earlier programs, we did not have to define how to read a line of text from the keyboardC++ provides this capability via the getline function of the <string> header file. A third motivation is to avoid repeating code. Also, dividing a program into meaningful functions makes the program easier to debug and maintain.

[Page 241]

Software Engineering Observation 6.2

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. Such functions make programs easier to write, test, debug and maintain.

Error-Prevention Tip 6.1

A small function that performs one task is easier to test and debug than a larger function that performs many tasks.

Software Engineering Observation 6.3

If you cannot choose a concise name that expresses a function's task, your function might be attempting to perform too many diverse tasks. It is usually best to break such a function into several smaller functions.

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 the 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 in a hierarchical manner. The boss function divides the responsibilities among the various worker functions. Note that worker1 acts as a "boss function" to worker4 and worker5.

Figure 6.1. Hierarchical boss function/worker function relationship.

Previous Page
Next Page