Previous: Chapter 3
Up: Table of Contents
Previous Page: Chapter 3
Next Page: 3.1 Designing the Algorithm with Functions

3 Designing Programs Top Down

As program tasks become more complex, it is easier to think about the problem and design the algorithm for the task at hand by breaking the complex task into smaller and simpler subtasks and then solve each of the subtasks independently. We do this all the time in everyday life, for example, suppose you need milk for your kid's dinner. A complete algorithm for solving this problem might begin:

find the car keys
         go to the garage
         get in the car
         put the key in the ignition
         start the car
         back the car out of the driveway
         ...
However; when we are worried about feeding the kids, we do not plan our algorithm in such detail. Instead our algorithm might be:
drive to the store
         buy milk
         drive home
where each of the steps in this algorithm is a subtask that may involve many steps itself.

We can do the same kind of modular design for our programming tasks: begin by thinking at a more abstract level about the major steps to be done, and then for each of these subtasks, design a separate algorithm to solve it. Each program subtask may then be implemented either by a set of statements or by a separate function. The advantages of a function are that it hides details of the actual computations from the main body of the code, and it can even be called upon to perform a subtask repeatedly by one or more other functions. In particular, well designed functions can be used in a variety of programs. (An example from the above might be driving; it is the same operation in the first and last steps of our algorithm; only the start and destination are different).

In this chapter we will discuss this method of modular design of algorithms and the programs that implement them. We will see how functions may be used in a C program, and how new functions may be defined in the program. As usual, we will look at both the syntax and semantics of this programming construct. Next we will look in more detail at the macro facilities provided by the C preprocessor (briefly discussed in Chapter ) and how these can be used to make programs more readable. The we describe how your programs can interact with the Operating System to perform I/O. Finally we continue our discussion of guidelines for debugging and common errors.



Previous: Chapter 3
Up: Table of Contents
Previous Page: Chapter 3
Next Page: 3.1 Designing the Algorithm with Functions

tep@wiliki.eng.hawaii.edu
Wed Aug 17 08:21:42 HST 1994