Topic 5: C++ Library Functions
Topic 5: C++ Library Functions
1
Functions
Code reuse is using program fragments that have already
been written and tested.
2
Function Call and Parameters
When we want a function to perform some task in our
program, we write a statement to call the function.
y = sqrt(x);
The function call activates the function sqrt, passing the parameter
x (value of x) to the function and returns the result.
5
Function Call, Parameters, and Result
A function can be thought of as a “black box” that
receives one or more input values and returns a single
output value.
Example: function sqrt
function sqrt
number square root of
parameter number
square root
passed as parameter
computation
input to returned as
function result
6
Function Call, Parameters, and Result
The assignment statement
y = sqrt(x);
is evaluated as follows:
1. Suppose x is 16.0, so function sqrt computes the square
root of 16.0 giving 4.0.
2. The function result 4.0 is assigned to variable y.
7
Function Call, Parameters, and Result
The assignment statement
z = 5.7 + sqrt(9.0);
is evaluated as follows:
1. Function sqrt computes the square root of 9.0 giving 3.0.
2. The values 5.7 and 3.0 are added together giving 8.7.
3. The result 8.7 is assigned to variable z.
8
Function Call, Parameters, and Result
Suppose a is 10.0 and b is 15.0. The assignment statement
sum_sqrt = sqrt( a + b );
is evaluated as follows:
1. The values of a and b (10.0 and 15.0) are added together
giving 25.0.
2. Function sqrt computes the square root of 25.0 giving
5.0
3. The result 5.0 is assigned to variable sum_sqrt.
9
Complete Program – version 1
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
double num;
cout << "Enter a number: ";
cin >> num;
return 0;
}
10
Complete Program – version 2
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
double num;
cout << "Enter a number: ";
cin >> num;
return 0;
}
11
Function Call, Parameters, and Result
Suppose num is 25.0. The statement
13
C++ Math Library Functions – cmath
Purpose: Example Parameter Result
Function Type Type
ceil(x) Returns the smallest integral double double
value that is not less than x:
If x is 45.23, ceil(x) is 46.0
floor(x) Returns the largest integral value double double
that is not greater than x:
If x is 45.23, floor(x) is 45.0
sqrt(x) Returns the non-negative square double double
root of x for x ≥ 0.0:
If x is 2.25, sqrt(x) is 1.5
14
C++ Math Library Functions – cmath
Purpose: Example Parameter Result
Function Type Type
sin(x) Returns the sine of angle x: double double
If x is 1.5708, sin(x) is 1.0 (radians)
cos(x) Returns the cosine of angle x: double double
If x is 0.0, cos(x) is 1.0 (radians)
tan(x) Returns the tangent of angle x: double double
If x is 0.0, tan(x) is 0.0 (radians)
log10(x) Returns the base-10 logarithm of double double
x for x > 0.0:
If x is 100.0, log10(x) is 2.0
15
C++ General Library Functions – cstdlib
Purpose: Example Parameter Result
Function Type Type
rand() Returns a random integer none int
between 0 and RAND_MAX
which is typically 32,767.
srand(x) Creates the first seed for a double none
random number series. The seed
is the value used by the random
number generator to calculate
the next number in the series.
16
Complete Program
#include <iostream>
#include <cstdlib>
#include <ctime>
int main(void)
{
srand(time(NULL)); // current time is the seed
cout << "Number generated: " << rand() << endl;
cout << "Next number generated: " << rand() << endl;
return 0;
}
17
Complete Program
18
Generate number in a determined range
A typical way to generate trivial pseudo-random numbers
in a determined range using rand is to use the modulo of
the returned value by the range span and add the initial
value of the range:
n1 = rand() % 100; // n1 in the range 0 to 99
n2 = rand() % 100 + 1; // n2 in the range 1 to 100
n3 = rand() % 37 + 1980; // n3 in the range 1980-2016
Formula:
number_generated = rand() % range + base;
where range = max – min + 1;
base = min;
19