Predefined C++ Functions
Most programming languages to have facilities to
name pieces of reusable code. The terms procedure,
subprogram, method and function all mean essentially
the same thing.
Like most languages, C++ provides a library of predefined
functions. The definitions of many common functions are
found in the cmath and cstdlib libraries. To access these
functions, your program must contain the directive(s)
#include
#include
in addition to using namespace std;
Some common functions are listed below. A larger
table of functions is included in the text on page 96.
Function
| Description
| Argument type
| Return type
| Header file
|
sqrt
| Square root
| double
| double
| cmath
|
pow
| Powers
| double
| double
| cmath
|
abs
| Absolute value for int
| int
| int
| cstdlib
|
exit
| End program
| int
| void
| cstdlib
|
assert
| Abort program with message
| boolean
| void
| assert
|
//------------------------------------------
// File: SqrtPowAbsExit.cpp
//
// Other required file header stuff here
//
// An example of the proper use of the
// sqrt(), pow(), abs() and exit() functions
//-------------------------------------------
#include
#include
#include
using namespace std;
int main ( )
{
// calling the functions with literal
cout << "The sqrt of 36 is " << sqrt(36.0) << endl;
cout << "36 to the 2nd power is " << pow(36.0, 2.0) << endl;
cout << "The absolute value of -44 is " << abs(-44) << endl;
cout << endl;
// calling the functions with variables
double x = 36.0;
double y = 2.0;
int z = -44;
cout << "The sqrt of " << x << " is " << sqrt( x ) << endl;
cout << x << " to the " << y << " power is " << pow(x, y) << endl;
cout << "The absolute value of " << z << " is " << abs( z ) << endl;
cout << endl;
exit ( -1 );
return 0;
}
The ANSI compiler is very picky about the arguments
that are passed to these functions (because of function "overloading"
which is discussed soon). For example, this code
// File: BadSqrt.cpp
//
// Other required file header stuff here
//
// An example to show potential compiler errors
// when using the wrong parameter types for some
// math functions
//----------------------------------------------
#include
#include
#include
using namespace std;
int main ( )
{
cout << "The sqrt of 36 is " << sqrt( 36 ) << endl;
cout << "36 to the 2nd power is " << pow( 36, 2 ) << endl;
cout << "The absolute value of -44 is " << abs( -44.0 ) << endl;
exit( -1 );
return 0;
}
generates this compiler error. What does it mean?
How do we fix it?
linux1[7]% g++ -ansi -Wall BadSqrtPow.cpp
BadSqrtPow.cpp: In function `int main()':
BadSqrtPow.cpp:10: call of overloaded `sqrt(int)' is ambiguous
/usr/include/bits/mathcalls.h:146: candidates are: double sqrt(double)
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.2.1/include/c++/cmath:465:
long double std::sqrt(long double)
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.2.1/include/c++/cmath:461:
float std::sqrt(float)
BadSqrtPow.cpp:12: call of overloaded `pow(int, int)' is ambiguous
/usr/include/bits/mathcalls.h:143: candidates are: double pow(double, double)
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.2.1/include/c++/cmath:427:
long double std::pow(long double, int)
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.2.1/include/c++/cmath:423:
float std::pow(float, int)
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.2.1/include/c++/cmath:419:
double std::pow(double, int)
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.2.1/include/c++/cmath:410:
long double std::pow(long double, long double)
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.2.1/include/c++/cmath:401:
float std::pow(float, float)
Last Modified: Monday, 28-Aug-2006 10:16:00 EDT