Wednesday, August 1, 2012

Programming and Problem Solving With C++

Programming and Problem Solving With C++ 3Rd Ed.

author : Dale, Nell B.; Weems, Chip.; Headington, Mark R

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.
In the Welcome program, execution begins with the first executable statement in the main function (the 
call to Print2Lines). When Print2Lines is called, control passes to its first statement and subsequent 
statements in its body. After the last statement in Print2Lines has executed, control returns to the main 
function at the point following the call (the output statement that prints ''Welcome Home!").
Function Parameters
Looking at the Welcome program, you can see that Print2Lines and Print4Lines are very similar functions. 
They differ only in the number of lines that they print. Do we really need two different functions in this 
program? Maybe we should write only one



No comments:

Post a Comment