0% found this document useful (0 votes)
22 views

C++ 1

Uploaded by

Виталий
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

C++ 1

Uploaded by

Виталий
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Functions

Because functions are the modules from which C++ programs are built and
because they are essential to C++ OOP definitions, you should become thoroughly
familiar with them. Some aspects of functions are advanced topics, so the main
discussion of functions comes later, in Chapters 7, “Functions: C++’s
Programming Modules,” and 8, “Adventures in Functions.” However, if we deal
now with some basic characteristics of functions, you’ll be more at ease and more
practiced with functions later. The rest of this chapter introduces you to these func-
tion basics.
C++ functions come in two varieties: those with return values and those
without them. You can find examples of each kind in the standard C++ library of
functions, and you can create your own functions of each type. Let’s look at a
library function that has a return value and then examine how you can write your
own simple functions.
Using a Function That Has a Return Value A function that has a return value
produces a value that you can assign to a variable. For example, the standard C/C+
+ library includes a function called sqrt() that returns the square root of a number.
Suppose you want to calculate the square root of 6.25 and assign it to the variable
x. You can use the following statement in your program:
x = sqrt(6.25); // returns the value 2.5 and assigns it to x
The expression sqrt(6.25) invokes, or calls, the sqrt() function. The expression
sqrt(6.25) is termed a function call, the invoked function is termed the called
function, and the function containing the function call is termed the calling
function. (See Figure 2.6.)
FIGURE 2.6 Calling a function.

The value in the parentheses (6.25, in this example) is information that is


sent to the function; it is said to be passed to the function. A value that is sent to a
function this way is called an argument or parameter. (See Figure 2.7.) The sqrt()
function calculates the answer to be 2.5 and sends that value back to the calling
function; the value sent back is called the return value of the function. Think of the
return value as what is substituted for the function call in the statement after the
function finishes its job. Thus, this example assigns the return value to the variable
x. In short, an argument is information sent to the function, and the return value is
a value sent back from the function.
X = sqrt(6+25);
FIGURE 2.7 Function call syntax.

That’s practically all there is to it, except that before the C++ compiler uses
a function, it must know what kind of arguments the function uses and what kind
of return value it has. That is, does the function return an integer? a character? a
number with a decimal fraction? a guilty verdict? or something else? If it lacks this
information, the compiler won’t know how to inter- pret the return value. The C++
way to convey this information is to use a function prototype statement.
Remember
A C++ program should provide a prototype for each function used in the program.
A function prototype does for functions what a variable declaration does for
variables: It tells what types are involved. For example, the C++ library defines the
sqrt() function to take a number with (potentially) a fractional part (like 6.25) as an
argument and to return a number of the same type. Some languages refer to such
numbers as real numbers, but the name C++ uses for this type is double. (You’ll
see more of double in Chapter 3.) The function prototype for sqrt() looks like this:
double sqrt(double); // function prototype
The initial double means sqrt() returns a type double value. The double in the
parentheses means sqrt() requires a double argument. So this prototype describes
sqrt() exactly as used in the following code:
double x; // declare x as a type double variable x = sqrt(6.25);
The terminating semicolon in the prototype identifies it as a statement and thus
makes it a prototype instead of a function header. If you omit the semicolon, the
compiler interprets the line as a function header and expects you to follow it with a
function body that defines the function.
When you use sqrt() in a program, you must also provide the prototype. You can
do this in either one of two ways:
•You can type the function prototype into your source code file yourself.
•You can include the cmath (math.h on older systems) header file, which has the
proto- type in it.
The second way is better because the header file is even more likely than
you to get the proto- type right. Every function in the C++ library has a prototype
in one or more header files. Just check the function description in your manual or
with online help, if you have it, and the description tells you which header file to
use. For example, the description of the sqrt() func- tion should tell you to use the
cmath header file. (Again, you might have to use the older math.h header file,
which works for both C and C++ programs.)
Don’t confuse the function prototype with the function definition. The
prototype, as you’ve seen, only describes the function interface. That is, it
describes the information sent to the function and the information sent back. The
definition, however, includes the code for the function’s workings—for example,
the code for calculating the square root of a number. C and C++ divide these two
features—prototype and definition—for library functions. The library files contain
the compiled code for the functions, whereas the header files contain the proto-
types.
You should place a function prototype ahead of where you first use the
function. The usual practice is to place prototypes just before the definition of the
main() function. Listing 2.4 demonstrates the use of the library function sqrt(); it
provides a prototype

You might also like