Using a Standard Library Function

advertisement
Using a Standard Library Function
As an example, let's look at the sqrt() function, which returns the square root of a
floating-point number. This skeleton code shows the essentials.
#include <math.h> // needed for sqrt()
...
double answer, somenum; // sqrt operates on type double
...
answer = sqrt(somenum); // find the square root
To use a library function, you'll need to look it up either in your compiler's online help or
in the manual. The first thing you'll need to know is the name of the appropriate header
file. Every library function requires that a header file be included before the function is
called; here it's math.h. Most of the math-related functions use this same header file.
The documentation will also tell you the data types of the function's arguments and return
value. The sqrt() function takes a single argument of type double and returns the same
type, so in this example, both somenum and answer are type double.
Return to Notes
Download