4.1 Top-Down Design

Remember that the way to write a program is to first design the method that the program will use and to write out this method in English, as if the instructions were to be followed by a human clerk. As we noted in Chapter 1, this set of instructions is called an algorithm. A good plan of attack for designing the algorithm is to break down the task to be accomplished into a few subtasks, decompose each of these subtasks into smaller subtasks, and so forth. Eventually, the subtasks become so small that they are trivial to implement in C++. This method is called top-down design. (The method is also sometimes called stepwise refinement, or more graphically, divide and conquer.)

Using the top-down method, you design a program by breaking the program’s task into subtasks and solving these subtasks by subalgorithms. Preserving this top-down structure in your C++ program makes the program easier to understand, easier to change if need be, and, as will become apparent, easier to write, test, and debug. C++, like most programming languages, has facilities to include separate subparts inside of a program. In other programming languages these subparts are called subprograms, procedures, or methods. In C++ these subparts are called functions.

One of the advantages of using functions to divide a programming task into subtasks is that the program becomes easier to understand, test, debug, and maintain. Additionally, dividing the task allows different people to work on the different subtasks. When producing a very large program, such as a compiler or office-management system, this sort of teamwork is needed if the program is to be produced in a reasonable amount of time. We will begin our discussion of functions by showing you how to use functions that were written by somebody else.