Sei sulla pagina 1di 26

LECTURE-16:

11/11/2015
FUNCTIONS

Muhammad Ahmad
Lecturer
1
CS Department
TODAY WE WILL COVER

11/11/2015
 Functions
 Built in Functions

 Function Definition

 Calling Functions

 User Defined Functions

2
MODULAR PROGRAMMING

11/11/2015
 Modular programming: breaking a
program up into smaller, manageable
functions or modules

 Function:a collection of statements to


perform a task
 Motivation for modular programming:
 Improves maintainability of programs
 Simplifies the process of writing programs

3
11/11/2015
4
Method Desc ription Example
ceil( x ) rounds x to the smallest integer ceil( 9.2 ) is 10.0
not less than x ceil( -9.8 ) is -9.0
cos( x ) trigonometric cosine of x cos( 0.0 ) is 1.0

11/11/2015
(x in radians)
exp( x ) exponential function ex exp( 1.0 ) is 2.71828
exp( 2.0 ) is 7.38906
fabs( x ) absolute value of x fabs( 5.1 ) is 5.1
fabs( 0.0 ) is 0.0
fabs( -8.76 ) is 8.76
floor( x ) rounds x to the largest integer floor( 9.2 ) is 9.0
not greater than x floor( -9.8 ) is -10.0
fmod( x, y ) remainder of x/y as a floating- fmod( 13.657, 2.333 ) is 1.992
point number
log( x ) natural logarithm of x (base e) log( 2.718282 ) is 1.0
log( 7.389056 ) is 2.0
log10( x ) logarithm of x (base 10) log10( 10.0 ) is 1.0
log10( 100.0 ) is 2.0
pow( x, y ) x raised to power y (xy) pow( 2, 7 ) is 128
pow( 9, .5 ) is 3
sin( x ) trigonometric sine of x sin( 0.0 ) is 0
(x in radians)
sqrt( x ) square root of x sqrt( 900.0 ) is 30.0
sqrt( 9.0 ) is 3.0
tan( x ) trigonometric tangent of x tan( 0.0 ) is 0 5
(x in radians)
Fig. 3.2 Math library func tions.
BUILT-IN-FUNCTIONS

11/11/2015
6
11/11/2015
DEFINING AND CALLING
FUNCTIONS

7
DEFINING AND CALLING FUNCTIONS

11/11/2015
 Function call: statement causes a function to
execute
 Function definition: statements that make up
a function

8
FUNCTION DEFINITION

11/11/2015
 Definition includes:
 return type: data type of the value that
function returns to the part of the program
that called it
 name: name of the function. Function
names follow same rules as variables
 parameter list: variables containing values
passed to the function
 body: statements that perform the
function’s task, enclosed in {}
9
FUNCTION DEFINITION

11/11/2015
10
FUNCTION RETURN TYPE

11/11/2015
 Ifa function returns a value, the type
of the value must be indicated:
int main()
 If a function does not return a value,
its return type is void:
void printHeading()
{
cout << "Monthly Sales\n";
}

11
CALLING A FUNCTION

11/11/2015
 To
call a function, use the function
name followed by () and ;
printHeading();

 When called, program executes the


body of the called function

 Afterthe function terminates,


execution resumes in the calling
function at point of call.
12
FUNCTIONS IN PROGRAM 6-1

11/11/2015
13
FLOW OF CONTROL IN PREVIOUS
PROGRAM

11/11/2015
14
11/11/2015
15
CALLING FUNCTIONS

11/11/2015
 main can call any number of functions
 Functions can call other functions
 Compiler must know the following about a
function before it is called:
 name
 return type
 number of parameters
 data type of each parameter

16
11/11/2015
17
PREPARE YOUR ANSWERS?

11/11/2015
 Is the following a function header or a
function call?
 calcTotal();

 Is the following a function header or a


function call?
 void showResults()

18
11/11/2015
19
TASK!

11/11/2015
 The following program skeleton determines
whether a person qualifies for a credit card.
To qualify, the person must have worked on
his or her current job for at least two years
and make at least $17,000 per year. Finish
the program by writing the definitions of the
functions qualify and noQualify. The
function qualify should explain that the
applicant qualifies for the card and that the
annual interest rate is 12%. The function
noQualify should explain that the applicant
does not qualify for the card and give a 20
general explanation why.
11/11/2015
21
11/11/2015
FUNCTION PROTOTYPES

22
FUNCTION PROTOTYPES

11/11/2015
 Ways to notify the compiler about a
function before a call to the function:

 Place function definition before calling


function’s definition
 Function prototypes are also known as
function declarations

 Use a function prototype (function


declaration) – like the function definition
without the body
 Header: void printHeading()
23
 Prototype: void printHeading();
11/11/2015
24
PROTOTYPE NOTES

11/11/2015
 Place prototypes near top of program

 Program must include either prototype or


full function definition before any call to
the function – compiler error otherwise

 When using prototypes, can place


function definitions in any order in source
file
25
11/11/2015
Questions

26

Potrebbero piacerti anche