Topic 5: C++ Library Functions

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Topic 5

C++ Library Functions

1
Functions
 Code reuse is using program fragments that have already
been written and tested.

 C++ promotes code reuse by providing many predefined


functions i.e. functions that are already written.
 These functions from part of the C++ standard library.

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.

 A function call activates a function.


 It consists of two parts: the function name and a list of
parameters enclosed in parentheses.

 The parameters represent the information to pass to the


function so that it can perform its task.
 Note: some authors use the name argument instead of
parameter.
3
Function Call, Parameters, and Result
 C++ also provides many functions that perform
mathematical computations and return results.

 For example, C++’s standard math library defines a


function named sqrt that performs the square root
computation.

 To use any functions from the standard math library in our


program, we must insert a preprocessor directive as
follows:
#include <cmath>
4
Function Call, Parameters, and Result
 We can call the function sqrt in an assignment statement:
function call

y = sqrt(x);

function name parameter

 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;

double sqrt_num = sqrt(num);

cout << "Square root of " << num


<< " is " << sqrt_num << endl;

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;

cout << "Square root of " << num


<< " is " << sqrt(num) << endl;

return 0;
}

11
Function Call, Parameters, and Result
 Suppose num is 25.0. The statement

cout << "Square root of " << num


<< " is " << sqrt(num) << endl;
is evaluated as follows:
1. Function sqrt computes the square root of 25.0 giving
5.0
2. The result 5.0 is displayed together with the value of
num.
Computer Screen
Square root of 25 is 5
12
C++ Math Library Functions – cmath
Purpose: Example Parameter Result
Function Type Type
abs(x) Returns the absolute value of its int int
type int argument:
If x is -5, abs(x) is 5
fabs(x) Returns the absolute value of its double double
type double argument:
If x is -8.432, fabs(x) is 8.432
pow(x, y) Returns xy. If x is negative, y double, double
must be an integral: double
If x is 0.16 and y is 0.5, pow(x,
y) is 0.4

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>

using namespace std;

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

You might also like