|
|
|
|
|
|
|
An Overview of User-Defined Functions |
|
|
|
|
|
|
|
|
Now that we've seen an example of how a program is written with functions, let's look briefly and informally at some of the more important points of function construction and use. |
|
|
|
|
|
|
|
|
Flow of Control in Function Calls |
|
|
|
|
|
|
|
|
We said that C++ function definitions can be arranged in any order, although main usually appears first. During compilation, the functions are translated in the order in which they physically appear. When the program is executed, however, control begins at the first statement in the main function, and the program proceeds in logical sequence. When a function call is encountered, logical control is passed to the first statement in that function's body. The statements in the function are executed in logical order. After the last one is executed, control returns to the point immediately following the function call. Because function calls alter the logical order of execution, functions are considered control structures. Figure 7-1 illustrates this physical versus logical ordering of functions. In the figure, functions A, B, and C are written in the physical order A, B, C but are executed in the order C, B, A. |
|
|
|
|
|
|
|
|
Figure 7-1
Physical Versus Logical Order of Functions |
|
|
|
|
|