< previous page page_103 next page >

Page 103
alpha = Cube(int1 * int1 + int2 * int2);
the expression in the parameter list is evaluated first, and only its result is passed to the function. For example, if int1 contains 3 and int2 contains 5, the above function call passes 34 as the parameter to Cube.
An expression in a function's parameter list can even include calls to functions. For example, we could use the Square function to rewrite the above assignment statement as follows:
alpha = Cube(Square(int1) + Square(int2));
Library Functions
Certain computations, such as taking square roots or finding the absolute value of a number, are very common in programs. It would be an enormous waste of time if every programmer had to start from scratch and create functions to perform these tasks. To help make the programmer's life easier, every C++ system includes a standard library-a large collection of prewritten functions that any C++ programmer may use. Here is a very small sample:
Header FileFunctionParameter Type(s)Result TypeResult
<stdlib.h>abs(i)intintAbsolute value of i
<math.h>cos(x)floatfloatCosine of x(x is in radians)
<math.h>fabs(x)floatfloatAbsolute value of j
<stdlib.h>labs(j)floatfloatAbsolute value of j
<math.h>pow(x, y)floatfloat
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
x raised to the power y (if x = 0.0, y must be a whole number)
<math.h>sin(x)floatfloatSince of x ( x is in radians)
<math.h>squrt(x)floatfloatSquare root of x ( xþ0.0)

(Technically, the entries marked float should all say double. These library functions perform their work using double precision floating point values. But because of type coercion, the functions work just as you would like them to when you pass float values to them.)
Using a library function is easy. First, you place an #include directive near the top of your program, specifying the appropriate header file. This directive ensures that the C++ preprocessor will insert declarations into your program that give the compiler some information about the function. Then,

 
< previous page page_103 next page >