3.0 cmath(round down)exit End program int void exit(1); None cstdlibNone int rand( ) Varies cstdlibrand Random numberunsigned srand Set seedvoid srand(42); None cstdlibintfor randAll these predefined functions require using namespace std; as well as an include directive.
Notice that the previous call to
pow returns
9.0, not
9. The function
pow always
arguments have a typereturns a value of type
double, not of type
int. Also notice that the function
powrequires two arguments. A function can have any number of arguments. Moreover,
every argument position has a specified type, and the argument used in a function call
should be of that type. In many cases, if you use an argument of the wrong type, some
automatic type conversion will be done for you by C++. However, the results may not
be what you intended. When you call a function, you should use arguments of the type
specified for that function. One exception to this caution is the automatic conversion
of arguments from type
int to type
double. In many situations, including calls to the
Predefined Functions 97
void F
UNCTIONSA void function performs some action, but does not return a value. For a void function, a func-tion call is a statement consisting of the function name followed by arguments enclosed in paren-theses and then terminated with a semicolon. If there is more than one argument, the arguments are separated by commas. For a void function, a function invocation (function call) is a state-ment that can be used like any other C++ statement. S
YNTAXFunction_Name(Argument_List);where the Argument_List is a comma-separated list of arguments:Argument_1, Argument_2, . . . , Argument_LastE
XAMPLEexit(1);function
pow, you can safely use an argument of type
int (or other integer type) when
an argument of type
double (or other floating-point type) is specified.
Many implementations of
pow have a restriction on what arguments can be used. In
restrictionson powthese implementations, if the first argument to
pow is negative, then the second argu-
ment must be a whole number. It might be easiest and safest to use
pow only when the
first argument is nonnegative.
■ PREDEFINED
void FUNCTIONS
A
void function performs some action, but does not return a value. Since it performs
an action, a
void function invocation is a statement. The function call for a
void func-
tion is written similar to a function call for a function that returns a value, except that it
is terminated with a semicolon and is used as a statement rather than as an expression.
Predefined
void functions are handled in the same way as predefined functions that
return a value. Thus, to use a predefined
void function, your program must have an
include directive that gives the name of the library that defines the function.
For example, the function
exit is defined in the library
cstdlib, and so a program
exitthat uses that function must contain the following at (or near) the start of the file:
#include <cstdlib>using namespace std;The following is a sample invocation (sample call) of the function
exit:
98 Function Basics
T
HEexit F
UNCTIONThe exit function is a predefined void function that takes one argument of type int. Thus, an invocation of the exit function is a statement written as follows:exit(Integer_Value);When the exit function is invoked (that is, when the above statement is executed), the program ends immediately. Any Integer_Value may be used, but by convention, 1 is used for a call to exit that is caused by an error, and 0 is used in other cases. The exit function definition is in the library cstdlib and it places the exit function in the stdnamespace. Therefore, any program that uses the exit function must contain the following two directives:An invocation of the
exit function ends the program immediately. Display 3.3 con-
tains a toy program that demonstrates the
exit function.
Note that the function
exit has one argument, which is of type
int. The argument
is given to the operating system. As far as your C++ program is concerned, you can use
Display 3.3 A Function Call for a Predefined void Function1 #include <iostream>This is just a toy example. It 2 #include <cstdlib>would produce the same output 3 using namespace std;if you omitted these lines.4 int main( )5 {6 cout << "Hello Out There!\n";7 exit(1);8 cout << "This statement is pointless,\n"9 << "because it will never be executed.\n"10 << "This is just a toy program to illustrate exit.\n";11 return 0;12 }SAMPLE DIALOGUEHello Out There!Predefined Functions 99
any
int value as the argument, but by convention,
1 is used for a call to
exit that is
caused by an error, and
0 is used in other cases.
A
void function can have any number of arguments. The details on arguments for
void functions are the same as they were for functions that return a value. In particular,
if you use an argument of the wrong type, then, in many cases, some automatic type
conversion will be done for you by C++. However, the results may not be what you
intended.
Self-Test Exercises
Bạn đang xem 3. - ABSOLUTE C PHẦN 3 PPS