# Chapter 11 – Using Libraries and Functions

## 11.1  Functions Defined

A function is a program or subroutine that may be used (or called) in any program you write.  There are many libraries that may be included in your programs, each containing many different functions.  For example, if you need to calculate the square root of a number, you do not need to write the code to make this calculation since there is already a function developed to do this.  Functions are a method of making code reusable --- code that may be used in many different programs.

Some software companies have developed and sell libraries of functions for various purposes.  For example, suppose you need to have your C++ draw bar and pie charts.  Instead of developing the programs yourself for drawing these, you could find a library already developed for this purpose.  In a later chapter, you will learn how to create your own functions and libraries.

Below is an example function call.  This is the square root function that is located in the <math.h> library.

cout << sqrt(25);

The above statement will output the number 5.  When you use this function, you put the name of the function (sqrt), and then in parenthesis you put what number to take the square root of (e.g. 25).  The function call returns the number 5.

## 11.2  Using Math.h Functions

The math.h library contains many arithmetic functions such as sine, cosine, square root, and natural logarithm.  The following program shows how to include the math.h library and use (or call) its square root function.

 Program  11.2a - Using the Square Root Function #include #include using namespace std; main() {   float A;   A = sqrt(16);   cout << A << endl; } Output 4

In the previous program, we must include the math.h library at the top to gain access to its functions.  In this program, we use (or call) the square root function.  The function name is sqrt.  We send this function a number (16) to use for calculating the square root.  The information you send the function is always enclosed in parenthesis and is called the parameter(s) or argument(s).  The square root function calculates the square root of 16 and returns the answer to your program.  In this program, the A will be assigned the value of 4.

Remember, functions are separate programs that you call from your main program.  You send them information (the parameters), and they return a result.  The information they return is always of a particular variable type such as int, string, float, etc.  The next program uses the power function.  It will raise the first number (2) to power of the second (5).  When using a function that takes two parameters, separate the numbers by a comma.

 Program  11.2b – Using the Power Function #include #include using namespace std;   main() {   int A;   A = pow(2,5);   cout << A << endl; } Output 32

The following table lists some of the functions found in the math.h library.

 Common Math.h Functions Function Comments sqrt(n) Calculates the square root of n. pow(n, p) Raises n to the power of p. exp(n) Calculates the exponential function – en. log(n) Calculates the natural logarithm using n. sin(n) Calculates the sine of n (in radians). cos(n) Calculates the cosine of n (in radians). tan(n) Calculates the tangent of n (in radians). floor(n) Rounds n down to the nearest integer – floor(1.6) is 1. ceil(n) Rounds n up to the nearest integer – ceil(2.45) is 3.

## 11.3  Using Stdlib.h Functions

This section lists some of the functions in the stdlib.h library.  One useful function contained in stdlib.h is system( ).  This function allows you to execute an operating system command.  How you use it depends on what operating system your compiler is located.  This function does not return a value and should be placed on a line by itself.  The following program assumes you are running your program on a Linux system and shows how to execute the date command.

 Program  11.2 – Using the System Function #include #include using namespace std;  main() {   cout << "The date and time is:\n";   system("date"); } Output The date and time is: Sun Aug  4 14:18:43 CDT 2002

The following table lists some of the functions found in the stdlib.h library.

 Common Stdlib.h Functions Function Comments system(s) Execute operating system command s. itoa(n) Convert integer n to a string. atoi(s) Convert string s to an integer. atof(s) Convert string s to a double. rand() Generate a random number (discussed in Random Numbers chapter). srand() Seed random number generator (discussed in Random Numbers chapter). min(n1, n2) Returns smallest of n1 and n2. max(n1, n2) Returns largest of n1 and n2. abs(n) Calculates absolute value of integer n.

## Exercises – Chapter 11

1. Write a program that prompts the user to enter two integers, and then outputs the first number raised to the power of the second.

2. Write a program that would have the following output:

a = Calculate sine
b = Calculate cosine
c = Calculate tangent
Please select an option: b
Enter a value: 2

The cosine of 2 is -0.416147

3.  What is wrong with the following statement?

char A = sqrt(9);