# Lab 3: Experiment 8

## Expressions Containing Functions

You've probably seen functions in the math courses that you've taken: the square root of a number or the sine of an angle. You may have noticed that there's no square root character on your computer keyboard (unless you're really special). So how do we compute these more complicated operations?

C++ borrowed the term function from mathematics to represent (nearly) the same thing. We'll look at writing our own functions in future labs, but for now we can use the functions that C++ gives us.

In C++, many 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 at least three reasons:

1. A library allows different programs to share the same function.
2. If the function were built into the language, every program would have the function, making the executable much large than it would have to be.
3. A library makes it easier to change the function.

Many of the libraries in C++ are originally C libraries. These libraries typically begin with the letter `c`, like `cmath` or `cctype`.

## 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
`fabs(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 arguments `x` and `y` are real numbers (i.e., `double`s).

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>`
at the beginning of your program before you use any of these functions. (With some compilers it may be necessary to specify `cmath.h`, instead of just `cmath`.)

Depending on your compiler, you may also have to tell the compiler where to find the definitions of these functions. (Check with your instructor on how to do this.)

Modify your program. Comment out the code you have for dealing with the integers `i` and `j`. Write code that declares `x`, reads in a value for `x`, and then prints the result of applying some functions to the value (see the table in the next question). Compile and execute your program.

Question #3.8.1: Use your program to fill out this table:
`x` function of x result
1000.0 the base-10 log of `x`
10.0 `x` raised to the power 3.0
3.14159 sine of `x`
3.14159 cosine of `x`
9.9 the ceiling of `x`
9.9 the floor of `x`

## 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. 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 `ch` is lower case; for all other characters, it returns `ch`
`tolower(ch)` returns the lower-case equivalent of `ch` if `ch` is uppercase; for all other characters, it returns `ch`
`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 (space, tab, newline, formfeed, carriage return, etc.)
`iscntrl(ch)` returns true if and only if `ch` is not a printable character
`ispunct(ch)` returns true if and only if `ch` is a punctuation character
In each of these functions, the argument `ch` is of type `char`.

The rule for libraries is simple:

 Helpful hint: Never, ever memorize a library.

You will end up memorizing some libraries. You won't be able to help yourself. But your purpose should never be to set out and memorize the contents of a library. Instead, you should be familiar with the available libraries, know basically what's in them, and (most importantly) know where you can find the details you need about them.

The name of this library is `cctype`.

Question #3.8.2: Considering how the `cmath` library is accessed in our program, how would you access the `cctype` library?

## Terminology

function, function library
Back to the Lab Exercise  |  Forward to the Next Experiment
© 2003 by Prentice Hall. All rights reserved.
Report all errors to Jeremy D. Frens.