C - Programming - C - Program - Chapter 07.pdf

(1269 KB) Pobierz
CHAPTER
7
U SER -D EFINED F UNCTIONS II
IN THIS CHAPTER, YOU WILL:
n Learn how to construct and use void functions in a program
n Discover the difference between value and reference
parameters
n Explore reference parameters and value-returning functions
n Learn about the scope of an identifier
n Examine the difference between local and global identifiers
n Discover static variables
n Learn function overloading
n Explore functions with default parameters
1247350560.050.png 1247350560.055.png 1247350560.056.png 1247350560.057.png 1247350560.001.png 1247350560.002.png 1247350560.003.png 1247350560.004.png 1247350560.005.png 1247350560.006.png 1247350560.007.png 1247350560.008.png 1247350560.009.png 1247350560.010.png 1247350560.011.png 1247350560.012.png 1247350560.013.png 1247350560.014.png 1247350560.015.png 1247350560.016.png 1247350560.017.png 1247350560.018.png 1247350560.019.png 1247350560.020.png 1247350560.021.png 1247350560.022.png 1247350560.023.png 1247350560.024.png 1247350560.025.png 1247350560.026.png 1247350560.027.png 1247350560.028.png 1247350560.029.png
346
|
Chapter 7: User-Defined Functions II
In Chapter 6, you learned how to use value-returning functions. In this chapter, you will
explore user-defined functions in general and, in particular, those C++ functions that do
not have a data type, called void functions.
Void Functions
Void functions and value-returning functions have similar structures. Both have a heading
and a body. You can place user-defined void functions either before or after the function
main . However, the program execution always begins with the first statement in the
function main . If you place user-defined void functions after the function main , you
should place the function prototype before the function main . A void function does not
have a data type. Therefore, functionType , that is, the return type, in the heading part
and the return statement in the body of the void functions are meaningless. However, in a
void function, you can use the return statement without any value; it is typically used to
exit the function early. Like value-returning functions, void functions may or may not
have formal parameters.
Because void functions do not have a data type, they are not used (called) in an
expression. A call to a void function is a stand-alone statement. Thus, to call a void
function, you use the function name together with the actual parameters (if any) in a
stand-alone statement.
Void Functions without Parameters
This section discusses void functions that do not have formal parameters.
FUNCTION DEFINITION
The general form (syntax) of the void function without formal parameters is as follows:
void functionName()
{
statements
}
where statements are usually declaration and/or executable statements. In C++, void
is a reserved word.
Like a variable name, a function name should be descriptive, so be sure to use meaningful
names when naming functions.
FUNCTION CALL
The function call has the following syntax:
functionName();
1247350560.030.png 1247350560.031.png 1247350560.032.png 1247350560.033.png 1247350560.034.png 1247350560.035.png 1247350560.036.png 1247350560.037.png 1247350560.038.png 1247350560.039.png 1247350560.040.png 1247350560.041.png 1247350560.042.png
 
Void Functions
|
347
Because these void functions do not have parameters, no value can be passed to them
(unless you use global variables, defined later in this chapter). Such functions are thus
usually good only for displaying information about the program or for printing state-
ments. Consider the following program.
EXAMPLE 7-1
Suppose you want to print the following banner to announce the annual spring sale.
(Programming Exercise 7 in Chapter 2 shows a similar output.)
******************************
******************************
*********** Annual ***********
******************************
******************************
******** Spring Sale **********
******************************
******************************
7
The banner starts with two lines of stars. After printing the line containing the text
Annual , you need to print another two lines of stars. After printing the line containing
the text Spring Sale , you need to print two more lines of stars. You can write a
function that prints two lines of stars and call it whenever you need it. The complete
program looks like this:
#include <iostream>
using namespace std;
void printStars();
int main()
{
printStars();
//Line 1
cout << "********** Annual
***********" << endl;
//Line 2
printStars();
//Line 3
cout << "******* Spring Sale **********" << endl;
//Line 4
printStars();
//Line 5
return 0;
}
void printStars()
{
cout << "******************************" << endl;
cout << "******************************" << endl;
}
1247350560.043.png 1247350560.044.png
348
|
Chapter 7: User-Defined Functions II
Sample Run:
******************************
******************************
*********** Annual ***********
******************************
******************************
******** Spring Sale **********
******************************
******************************
In Line 1, the function printStars is called and it outputs the first two lines of the
output. The statement in Line 2 outputs the line of stars containing the text Annual ,
which is the third line of the output. In Line 3, the function printStars is called again
and it outputs the next two lines of the output. The statement in Line 4 then outputs the
line of stars containing the text Spring Sale , which is the sixth line of the output. In
Line 5, the function printStars is called again and it outputs the last two lines of the
output.
The statement printStars(); in the function main is a function call.
In the previous program, you can replace the function printStars with the following
function:
void printStars()
{
int stars, lines;
for (lines = 1; lines <= 2; lines++)
//Line 6
{
for (stars = 1; stars <= 30; stars++)
//Line 7
cout << ' * ';
//Line 8
cout << endl;
//Line 9
}
}
In this function definition, the outer for loop (Line 6) has two iterations. For each
iteration of the outer for loop, the body of the inner for loop (Line 7) executes 30
times, each time printing a star. The statement in Line 8 prints each star. The output
statement in Line 9 positions the cursor at the beginning of the next line on the standard
output device. Because the outer for loop has two iterations, this function outputs two
lines of stars with 30 stars in each line.
1247350560.045.png 1247350560.046.png 1247350560.047.png 1247350560.048.png 1247350560.049.png
 
Void Functions
|
349
You would agree that the definition of the function printStars using for loops to
output two lines of stars with 30 stars in each line is much easier to modify than the
definition of printStars given earlier. If you need to output five lines of stars instead of
two lines, for instance, you can replace the number 2 (in the first for loop, Line 6) with
the number 5 . Similarly, if you need to output 40 stars instead of 30 stars in each line,
you can replace the number 30 (in the second for loop, Line 7) with the number 40 .
Furthermore, as you will soon discover, the definition of the function printStars
using for loops is much easier to modify in order to establish the communication links
with the calling function (such as the function main ) and to do different things each time
the function printStars is called.
In the previous program, the function printStars always prints two lines of stars, with
30 stars in each line. Now suppose that you want to print the following pattern (a triangle
of stars):
*
* *
* * *
* * * *
7
You could write a function similar to the function printStars .However,ifyou
need to extend this pattern to 20 lines, the function printStars will have 20 lines.
Everytimeyoucallthefunction printStars , it prints the same number of lines;
the function printStars is inflexible. However, if you can somehow tell the
function printStars how many lines to print, you can enhance its flexibility
considerably. A communication link must exist between the calling function and
the called function.
Void Functions with Parameters
The previous section discussed void functions without parameters and pointed out the
limitations of such functions. In particular, you learned that no information can be passed
in and out of void functions without parameters. The communication link between the
calling function and the called function is established by using parameters. This section
discusses void functions with parameters.
FUNCTION DEFINITION
The function definition of void functions with parameters has the following syntax:
void functionName(formal parameter list)
{
statements
}
where statements are usually declaration and/or executable statements.
1247350560.051.png 1247350560.052.png 1247350560.053.png 1247350560.054.png
 
Zgłoś jeśli naruszono regulamin