Chapter 5 : Functions 5.1. Motivation: the need for - TopicsExpress



          

Chapter 5 : Functions 5.1. Motivation: the need for functions. The basic property of functions is that they enable us to break a pro- gram down into a number of smaller units. There are several different reasons why programs should be broken down in this way. Different users attach different importance to these reasons; some of the considera- tions below may be considered irrelevant by some practitioners. (i) One approach starts from the problem which the program is intended to solve. Most problems naturally break down into sub-problems. Many methods for problem solving or system analysis rely on this property. A large problem is broken down into sub-problems; each non-trivial sub-problem is broken down into smaller sub-sub- problems ... Each solution of a sub-problem will be represented by an appropriate piece of program called a function. This may be called "top-down" problem analysis. (ii) Most problems in the real world of industry or commerce are large and complex. Large problems must be implemented by a team, not an individual. The problem therefore needs to be broken down in a way appropriate for team implementation. This may be done by looking at the different objects involved in the problem and their associ- ated operations; this may be called an "object-oriented" approach to programming. (iii)When you write a program, you will often find that similar or identical code is required at several places in the program. The use of a function allows this code to be written just once, and to be called up wherever it is required. Re-usability If a solution has been created for a particular sub-problem (an imple- mentation of the solution involving means for storing the related data items, and the means of accessing them in an appropriate way) may be useful in more than one problem. An example is the sorting of a number of items into a particular order; sorting is a basic operation which is needed at many points in many computer problems. Units which solve frequently occurring sub-problems can therefore be re- used in appropriate places in a variety of larger problems. (i) They may be used in order to save effort in re-solving that partic- ular problem, and in re-programming the solution. (ii) They may be used in order to produce better quality systems, on the assumption that the solution being re-used was written and tested (by someone else) to a high quality standard when it was originally written. In this C++ course we teach just the programming techniques needed for producing useful re-usable code; we are not emphasising just WHY you may choose to break down your problem into these particular units. We need what are called "functions" and "structures" in C++; in the next course you will learn techniques for using functions and structures as the basis of an object-oriented approach to the design of programs; they can be used in other design methodologies too. In this section of the course, we teach the techniques for implementing functions. For those who have used other programming languages, the concept of a "function" in C++ corresponds to a "procedure" or "subrou- tine" elsewhere. 5.2. A simple example of a function // First the declaration and definition // of two functions void dothis () { cout numb, numb != 0 ) { // This is the call calcs = halberstam( numb ); cout
Posted on: Sat, 06 Jul 2013 15:55:00 +0000

Trending Topics



Recently Viewed Topics




© 2015