Experiment 8: Expressions Containing Functions


The idea of a function, such as the square root of a number, or the sine of an angle should be a familiar one. In C++, most functions are stored in function libraries: software modules in which useful functions can be stored for reuse. C++ stores functions in libraries (instead of building them into the language) for two reasons:

  1. Doing so allows different programs to share the same function; and
  2. A program that does not use such functions takes up less space than a program that does use such functions. (This would not necessarily be the case if functions were built-in.)
Some of the C++ function libraries are inherited from the parent language, C; others are original to C++. You can usually tell the difference because the names of the ones that are inherited from C begin with the letter c.

The C Math Library

The library that provides mathematical functions like sine, cosine, logarithm, and so on is generally known as the math library. Most of its functions should be familiar to students who have had a course in trigonometry. Some of the more common functions there include:

Function Description
abs(x) Absolute value of x
sin(x) Sine of x (in radians)
cos(x) Cosine of x (in radians)
tan(x) Tangent of x (in radians)
asin(x) Inverse sine of x (in radians)
acos(x) Inverse cosine of x (in radians)
atan(x) Inverse tangent of x (in radians)
sinh(x) Hyperbolic sine of x (in radians)
cosh(x) Hyperbolic cosine of x (in radians)
tanh(x) Hyperbolic tangent of x (in radians)
log10(x) Base 10 logarithm of x
log(x) Base e (natural) logarithm of x
pow(x,y) x raised to power y
exp(x) e raised to the power x
sqrt(x) Square root of x
ceil(x) Smallest integer not less than x
floor(x) Largest integer not greater than x

In each of these functions, the argument x is a real (double) value.

The declarations of these functions are stored in the system file cmath (indicating that C++ inherited the library from C), and so it is necessary to place the line

   #include <cmath>
before the beginning of one's main program. (On some compilers it may be necessary to specify cmath.h, instead of just cmath.)

The definitions of these functions are stored in the math library, which is automatically searched by most C++ compilers. (If yours does not search it, your program will compile correctly but will generate linking errors. If this occurs, you will need to tell your compiler to search the math library, either with a command-line switch (e.g., -lm) or by clicking the appropriate check box in your Options->Linking dialog box.)

Modify express.cpp as needed to find the following values (Hint: do all of them simultaneously, to reducing the time spent recompiling):
____________ the base-10 log of 1000.0 ____________ 10.0 raised to the power 3.0
____________ the sine of pi (3.14159) radians ____________ the cosine of pi (3.14159) radians
____________ the base 10 logarithm of 1000 __________ 10 raised to the power 3
____________ the ceiling of 9.9 ____________ the ceiling of 9.1

The C Character Type Library

In addition to a library of mathematical functions, C++ also provides a library containing a rich assortment of character-manipulating functions. (Actually, most of these just look like functions, but we won't make a distinction here.) This library is called the character type library. The functions provided by this library include:
Function Description
toupper(ch) returns the upper-case equivalent of ch, if and only if ch is lower-case
tolower(ch) returns the lower-case equivalent of ch, if and only if ch is upper-case
isupper(ch) returns true if and only if ch is one of 'A' through 'Z'
islower(ch) returns true if and only if ch is one of 'a' through 'z'
isdigit(ch) returns true if and only if ch is one of '0' through '9'
isalpha(ch) returns true if and only if ch is one of 'A' through 'Z' or 'a' through 'z'
isspace(ch) returns true if and only if ch is a white-space character (SPC, TAB, '\n', FORMFEED, CR, or VTAB)
iscntrl(ch) returns true if and only if ch is not a printable character (SPC)
ispunct(ch) returns true if and only if ch is a punctuation character (printable, but not SPC, a digit, or a letter)

In each of these functions, the argument ch is a character (char) value.

For now, just take a moment to read through the description of each function and try to remember the general capabilities the library provides. Then if you encounter a problem in which that operation is needed and you remember that there is a predefined function for that operation, you make the mistake of "reinventing the wheel."

The declarations of these functions are stored in the system file cctype (indicating that C++ inherited the library from C), and so it is necessary to place the line

   #include <cctype>
before the beginning of one's main program. (Older compilers may use ctype.h instead of cctype.)

From this exercise, you should now have an intuitive idea of what a function library is. You should also know that among the many function libraries C++ provides are a library of mathematical functions, and a library of character-manipulating functions.


Back to the Experiment List

Forward to the Next Experiment


Copyright 1998 by Joel C. Adams. All rights reserved.