📄 chap04.txt
字号:
Chapter 4 - Functions
When you compile and execute this program, you will find that the
first variable got changed in the function but was returned to its
original value when we returned to the main program. The second
variable however, was changed in the function and the new value was
reflected back into the variable in the main program which we can
see when the values are listed on the monitor.
DEFAULT PARAMETERS
_________________________________________________________________
Examine the file named DEFAULT.CPP for an ===============
example of the use of default parameters in C++. DEFAULT.CPP
This program really looks strange since it ===============
contains default values for some of the
parameters in the prototype, but these default
values are very useful as we will see shortly.
This prototype says that the first parameter named length must be
given for each call of this function because a default value is not
supplied. The second parameter named width, however, is not
required to be specified for each call, and if it is not specified,
the value 2 will be used for the variable width within the
function. Likewise, the third parameter is optional, and if it is
not specified, the value of 3 will be used for height within the
function.
In line 11 of this program, all three parameters are specified so
there is nothing unusual about this call from any other function
call we have made. Only two values are specified in line 12
however, so we will use the default value for the third parameter
and the system acts as if we called it with get_value(x, y, 3)
since the default value for the third value is 3. In line 13, we
only specified one parameter which will be used for the first
formal parameter, and the other two will be defaulted. The system
will act as if we had called the function with get_volume(x, 2, 3).
Note that the output from these three lines is reversed. This will
be explained shortly.
There are a few rules which should be obvious but will be stated
anyway. Once a parameter is given a default value in the list of
formal parameters, all of the remaining must have default values
also. It is not possible to leave a hole in the middle of the
list, only the trailing values can be defaulted. Of course, the
defaulted values must be of the correct types or a compiler error
will be issued. The default values can be given in either the
prototype or the function header, but not in both. If they are
given in both places, the compiler must not only use the default
value, but it must carefully check to see that both values are
identical. This could further complicate an already very
complicated problem, that of writing a C++ compiler.
As a matter of style, it is highly recommended that the default
values be given in the prototype rather than in the function. The
Page 4-5
Chapter 4 - Functions
reason will be obvious when we begin using object oriented
programming techniques.
WHY IS THE OUTPUT SCRAMBLED?
_________________________________________________________________
When the compiler finds a cout statement, the complete line of code
is initially scanned from right to left to evaluate any functions,
then the data is output field by field from left to right.
Therefore in line 11, get_value() is evaluated with its internal
output displayed first. Then the fields of the cout are displayed
from left to right with "Some box data is" displayed next.
Finally, the result of the return from get_value() is output in int
format, the type of the returned value. The end result is that the
output is not in the expected order when lines 11 through 13 are
executed. (The output is not what you would intuitively expect to
happen so appears to be a deficiency in the language. A call to
Borland International, the writers of Turbo C++ and Borland C++,
verified that this is operating correctly.)
Lines 15 through 18 are similar to any two of the lines of code in
lines 11 through 13, but are each separated into two lines so the
output is in the expected order.
Be sure to compile and execute DEFAULT.CPP after you understand it.
Note that the funny output order will appear again later in this
tutorial.
VARIABLE NUMBER OF ARGUMENTS
_________________________________________________________________
Examine the program named VARARGS.CPP for an ===============
illustration of the use of a variable number of VARARGS.CPP
arguments in a function call. ===============
We have gone to a lot of trouble to get the
compiler to help us by carefully checking how many parameters we
use in the function calls and checking the types of the parameters.
On rare occasion, we may wish to write a function that uses a
variable number of parameters. The printf() function is a good
example of this. ANSI-C has a series of three macros available in
the "stdarg.h" header file to allow the use of a variable number
of arguments. These are available for use with C++ also, but we
need a way to eliminate the strong type checking that is done with
all C++ functions. The three dots illustrated in line 6 will do
this for us. This prototype says that a single argument of type
int is required as the first parameter, then no further type
checking will be done by the compiler.
You will note that the main program consists of three calls to the
function, each with a different number of parameters, and the
system does not balk at the differences in the function calls. In
Page 4-6
Chapter 4 - Functions
fact, you could put as many different types as you desire in the
calls. As long as the first one is an int type variable, the
system will do its best to compile and run it for you. Of course
the compiler is ignoring all type checking beyond the first
parameter so it is up to you to make sure you use the correct
parameter types in this call.
In this case the first parameter gives the system the number of
additional parameters to look for and handle. In this simple
program, we simply display the numbers on the monitor to illustrate
that they really did get handled properly.
Of course, you realize that using a variable number of arguments
in a function call can lead to very obscure code and should be used
very little in a production program, but the capability exists if
you need it. Be sure to compile and execute this program.
FUNCTION NAME OVERLOADING
_________________________________________________________________
Examine the file named OVERLOAD.CPP for an ================
example of a program with the function names OVERLOAD.CPP
overloaded. This is not possible in ANSI-C, but ================
is perfectly legal and in fact used quite
regularly in C++. At first this will seem a bit
strange, but it is one of the keystones of object oriented
programming. You will see its utility and purpose very clearly in
later chapters of this tutorial.
You will notice in this example program that there are three
functions, in addition to the main function, and all three have the
same name. Your first question is likely to be, "Which function
do you call when you call do_stuff()?" That is a valid question
and the answer is, the function that has the correct number of
formal parameters of the correct types. If do_stuff() is called
with an integer value or variable as its actual parameter, the
function beginning in line 23 will be called and executed. If the
single actual parameter is of type float, the function beginning
in line 28 will be called, and if two floats are specified, the
function beginning in line 34 will be called.
It should be noted that the return type is not used to determine
which function will be called. Only the formal parameters are used
to determine which overloaded function will be called.
The keyword overload used in line 4 tells the system that you
really do intend to overload the name do_stuff, and the overloading
is not merely an oversight. This is only required in C++ version
1.2. C++ version 2.0 and greater do not require the keyword
overload but allows it to be used optionally in order to allow the
existing body of C++ code to be compatible with newer compilers.
It is not necessary to use this keyword because, when overloading
Page 4-7
Chapter 4 - Functions
is used in C++, it is generally used in a context in which it is
obvious that the function name is overloaded.
The actual selection of which function to actually call is done at
compile time, not at execution time so the program is not slowed
down. If each of the overloaded function names were changed to
different names, each being unique, there would be no difference
in execution size or speed of the resulting program.
Overloading of function names may seem very strange to you, and it
is strange if you are used to the rules of K&R or ANSI-C
programming. As you gain experience with C++, you will feel very
comfortable with this and you will use it a lot in your C++
programming.
Note the use of the keyword const used in some of the function
prototypes and headers. Once again, this prevents the programmer
from accidentally changing the formal parameter within the
function. In a function as short as these, there is no real
problem with an accidental assignment. In a real function that you
occasionally modify, you could easily forget the original intention
of the use of a value and attempt to change it during an extended
debugging session.
PROGRAMMING EXERCISES
_________________________________________________________________
1. Change the type of wings in the prototype of PROTYPE1.CPP to
float so that it disagrees with the function definition to see
if you get a compilation error.
2. Change the function definition in PROTYPE1.CPP to agree with
the changed prototype. Compile and execute the program
without changing the calls in lines 12 and 13. Explain the
results.
3. In DEFAULT.CPP, remove the default value from the prototype
for height only to see what kind of compiler error you get.
Only the last values of the list can be defaulted.
4. In OVERLOAD.CPP, change the names of the three functions so
that each is a unique name and compare the size of the
resulting executable file with that given for the present
program.
Page 4-8
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -