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
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();
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;
}
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.
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.
Plik z chomika:
Januszek66
Inne pliki z tego folderu:
Back Seat_ A Mumbai Tale - Aditya Kripalani.mobi
(755 KB)
Brief Wondrous Life of Oscar Wao, The - Junot Diaz.opf
(3 KB)
Don't Make Me Think, Revisited_ - Steve Krug.mobi
(9256 KB)
M. T. Anderson - Norumbegan 03 - The Empire of Gut and Bone # (v5.0).epub
(2209 KB)
M. T. Anderson - Norumbegan 02 - The Suburb Beyond the Stars # (v5.0).epub
(2105 KB)
Inne foldery tego chomika:
Dokumenty
Galeria
LUDLUM ROBERT
Midi - Kar
Mszał Rzymski PL
Zgłoś jeśli
naruszono regulamin