Next: The mathematical function library Up: Top-down design using Functions Previous: Top-down design using Functions

The need for functions

A rationale for the use of functions has been given above. Basically they save work in that having solved a problem once it need not be solved again if there exists a function to solve the problem given the particular parameters for this instance of the problem. In addition the function can be comprehensively tested and hence a possible source of error is eliminated in future programs. These reasons are now expanded upon:

  1. When solving large problems it is usually necessary to split the problem down into a series of sub-problems, which in turn may be split into further sub-problems etc. This is usually called a top-down approach. This process continues until problems become of such a size that they can be solved by a single programmer. This top-down approach is essential if the work has to be shared out between a team of programmers, each programmer ending up with a specification for a part of the system which is to be written as a function (or functions). While writing a single function the programmer is able to concentrate on the solution of this one problem only and is thus more likely to be able to solve the problem and make less errors. This function can now be tested on its own for correctness.
  2. In a particular organisation or industry it may be found that in carrying out the top-down approach in 1 some tasks occur very frequently. For example the operation of sorting a file of data into some order occurs frequently in data-processing applications. Thus a library of such commonly used functions can be built up and re-used in many different programs. This obviously saves much work and cuts down errors if such functions have already been well tested.
  3. There are many very specialised problem areas, not every programmer can know every area. For example many programmers working in scientific applications will frequently use mathematical function routines like sine and cosine, but would have no idea how to write such routines. Similarly a programmer working in commercial applications might know very little about how an efficient sorting routine can be implemented. However a specialist can write such routines, place them in a public library of functions and all programmers can benefit from this expertise by being able to use these efficient and well tested functions.

Before looking at how functions are implemented in C++ the use of the mathematical function routines supplied in C++ is considered.



Next: The mathematical function library Up: Top-down design using Functions Previous: Top-down design using Functions