Chapter Summary

I l @ ve RuBoard

Chapter Summary

What have you accomplished? On the practical side, you have seen how to develop a random-number generator and an integer-sorting program. In the process, you met getint() and sort () functions that you can use in other programs. On the educational side, you have learned some general principles and concepts useful in designing complex programs.

Programs should be designed rather than allowed to evolve through some random process of growth, trial, and error. You should think carefully about the form and content of input and output for a program. You should break the program into well-defined tasks and then write the code for these tasks separately, keeping in mind how they interface with one another. The idea is to achieve modularity. When necessary, break a module into smaller modules, and use functions to enhance the program's modularity and clarity.

When designing a program, try to anticipate what might go wrong and then write the program accordingly . Use error-trapping to circumvent potential problems or, at least, to alert the user if a problem shows up. It's much better to give the user a second chance to enter data than to let the program crash in ignominy.

When designing a function, first decide how it will interact with the calling function. Decide what information flows in and what information flows out. What will the arguments be? Will you use pointers, or return , or both? After you have these design parameters in mind, you can turn your attention to the mechanics of the function.

As you put these ideas to use, you'll produce programs with greater reliability, and you might acquire a body of functions that you can use in other programs. If so, your programming will take less time.

Don't forget about storage classes. Variables can be defined outside functions, in which case they are external (or global) and are available to more than one function. Variables defined within a function are local to that function and are not known to other functions. When possible, use local variables. This keeps variables in one function from being contaminated by the actions of other functions.

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net