Previous: 3.6 Common Errors
Up: 3 Designing Programs Top Down
Next: 3.8 Exercises
Previous Page: 3.6 Common Errors
Next Page: 3.8 Exercises

3.7 Summary

This chapter has presented a key concept in the design of good programs; namely, top down design. Beginning with the algorithm, complex programming tasks are divided into logical subtasks which themselves may be further divided. This structured design is a form of information hiding - hiding the details of an operation in its abstraction. We have described how these logical subtasks may be implemented using functions in C. A function is a block of code, which when given some information, performs some operations on the data and returns a value. To invoke (call) a function, use a statement with the form:

where each argument may be an arbitrary expression. A function is defined by specifying a function_header and a function_body. A function header takes the form: and a function body is simply a block containing local variable declarations followed by executable statements to perform the task of the function.

We saw that the <parameter>'s in the function header are really just special forms of variable declarations; containing a type specifier and an identifier. They declare additional local variables within the function which are initialized to the values passed as arguments in the call. We also saw how declaration statements can initialize variables when a block is entered:

Remember, all local variables local to a function may be accessed ONLY within the body of the function, not by functions calling this function and not by functions called by this function.

The value returned by a function is specified in a return statement of the form:

    <expression>;
If the last statement of the function is reached without executing a return statement, the function returns with an unknown return value.

Next we discussed another form of information hiding using compiler directives processed by the C preprocessor. These included macros, with and without arguments, including header files, and conditional compilation.

  • #define <symbol_name> <substitution_string>
  • #include <filename>
  • #include "filename "
  • #ifdef <identifier>
(and other variations of the #if directive).

Finally, we described the relationship between I/O in C and files, including end of file and redirection of standard input and output files.



Previous: 3.6 Common Errors
Up: 3 Designing Programs Top Down
Next: 3.8 Exercises
Previous Page: 3.6 Common Errors
Next Page: 3.8 Exercises

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