|
|
|
|
|
|
|
You have been using C++ functions since we introduced standard library routines such as sqrt and abs in Chapter 3. By now, you should be quite comfortable with the idea of calling these subprograms to perform a task. So far, we have not considered how the programmer can create his or her own functions other than main. That is the topic of this chapter and the next. |
|
|
|
|
|
|
|
|
You might wonder why we waited until now to look at user-defined subprograms. The reason, and the major purpose for using subprograms, is that we write our own value-returning functions and void functions to help organize and simplify large and complex programs. Until now, our programs have been relatively small and simple, so we didn't need to write subprograms. Now that we've covered the basic control structures, we are ready to introduce subprograms so we can begin writing larger and more complex programs. |
|
|
|
|
|
|
|
|
Top-Down Structured Design with Void Functions |
|
|
|
|
|
|
|
|
As a brief refresher, let's review the two kinds of subprograms that the C++ language works with: value-returning functions and void functions. A value-returning function receives some data through its parameter list, computes a single function value, and returns this function value to the calling code. The caller invokes (calls) a value-returning function by using its name and parameter list in an expression: |
|
|
|
|
|
|
|
|
In contrast, a void function (procedure, in some languages) does not return a function value. Nor is it called from within an expression. Instead, the function call appears as a complete, stand-alone statement. An example is the get function associated with the istream and ifstream data types: |
|
|
|
|
|
|
|
|
In this chapter, we concentrate exclusively on creating our own void functions. In Chapter 8, we examine how to write value-returning functions. |
|
|
|
|
|
|
|
|
From the early chapters on, you have been designing your programs as collections of modules. Many of these modules are naturally implemented as user-defined void functions. We now look at how to turn the modules in your algorithms into user-defined void functions. |
|
|
|
|
|