Sei sulla pagina 1di 4

Lesson 3: Predefined Functions

Slide 1: Lesson Outline


This document is designed to help you take notes while watching the videos for this
lesson. Print these slides before going to the Video Lectures.
Note: Reviewing this material is not sufficient to fully understand the topic; you should
also complete the readings and exercises.
Review these slides

Predefined Functions (slides 2-5)


Functioning of Predefined Functions (Slides 6-9)

Slide 2: Predefined Functions

We have functions provided by C++ such as sqrt called predefined functions.

When using a predefined function, need to include the library it is found in:
#include <cmath>

Slide 3: Predefined Functions (cont'd)

A function call is a program statement or expression that transfers control to a


function so that it will perform its particular subtask.
For example:
x = sqrt( 25 ); // function call

Any value that a function call supplies to the function is known as an argument of
the function call.

Any results calculated by and returned by the function call are known as the return
value.

Lesson 3

Slide 4: Predefined Functions (cont'd)

General syntax for a function call is:


function-name(argument1, argument2, ...)

Input value to function can be a single variable or an expression.

For example:
sqrt (x)
sqrt(b * b - 4 * a * c)

Slide 5: Predefined Functions (cont'd)

Each function takes input of a particular type. For example: sqrt expects a number.

Each function returns a value of a particular type.

Example:
a = 75;
cout << "Square root of 100 is" << sqrt(100)
<< endl;
cout << "Another root:" <<sqrt(2 * a + 100)
<< endl;

Lesson 3

Slide 6:
6 Functio
oning of Predefine
P
ed Functiions
When we
w use sqrt((x) inside main():
m

int ma
ain()
{
......
0);
x = sqrt(100
......

rn 0;
retur
}

7 Functio
oning of Predefine
P
ed Functiions (con
nt'd)
Slide 7:
When we
w use sqrt((x) inside main:
m
1) Inputt value or parameter
p
value
v
x is pa
assed to sq
qrt function..
ution of ma
ain() functio
on is tempo
orarily suspeended.
2) Execu
f
beccomes activ
ve and com
mputes the o
output or re
return valuee using som
me
3) sqrt function
method unknown to
t us.
r
value
e is transferrred back to
o main() fu
unction, which resumes the
4) The return
computa
ation using the return value.

Lesson 3

Slide 8: Functioning of Predefined Functions (cont'd)


Some of the predefined functions available in cmath:
Return Type Function Call
int
long

abs(int)
labs(long)

double

fabs(double)

double

cos(x) // cos in radians

double

fmod(double,double)

Slide 9: Functioning of Predefined Functions (cont'd)


Some of the predefined functions available in cmath:

Lesson 3

Return Type
double

Function Call
sqrt(double)

double

pow(double,double)

double

exp(double)

double

ceil(double)

double

floor(double)

Potrebbero piacerti anche