Академический Документы
Профессиональный Документы
Культура Документы
Chapter 6
1
Contents
6.1 Introduction
6.2 Function and parameter declaration
- Predefined function
- User-defined function
- Function prototype
- Flow of execution
June 4, 2020 2
Introduction
⚫ Functions are like building blocks
⚫ They allow complicated programs to be divided
into manageable pieces
⚫ Some advantages of functions:
⚫ A programmer can focus on just that part of the
program and construct it, debug it, and perfect it
⚫ Different people can work on different functions
simultaneously
⚫ Can be used in more than one place in a program or in
different programs
June 4, 2020 3
Introduction (cont.)
⚫ Functions :
⚫ Called modules
June 4, 2020 4
Function and parameter
declaration
⚫ There are two types of function :
⚫ predefined functions : carry out tasks that have
been preprogrammed in the C++ program
⚫ user-defined functions : users can define how
the output is produced
⚫ A function carries out its task when it is called by
the main() function.
⚫ Function call statement: call a function using the
function name and pass data (if any) to it.
June 4, 2020 5
Function and parameter
declaration (cont.)
Pre-defined function :
⚫ Predefined functions are organized into separate
libraries
⚫ I/O functions are in iostream header
⚫ Math functions are in cmath header
⚫ Some of the predefined mathematical functions are:
sqrt(x)
pow(x,y)
floor(x)
June 4, 2020 6
Function and parameter
declaration (cont.)
Pre-defined function :
⚫ pow(x,y) :
June 4, 2020 7
Function and parameter
declaration (cont.)
Pre-defined function :
June 4, 2020 8
Function and parameter
declaration (cont.)
Pre-defined function :
⚫ floor(48.79) is 48.0
June 4, 2020 9
Function and parameter
declaration (cont.)
June 4, 2020 10
Function and parameter
declaration (cont.)
User-defined function :
⚫ Void functions: do not have a return type
June 4, 2020 12
Function and parameter
declaration (cont.)
User-defined function :
⚫ Syntax:
June 4, 2020 13
Function and parameter
declaration (cont.)
User-defined function :
⚫ formal parameter : a variable declared in the
function heading; can be empty
functionType functionName()
functionName()
June 4, 2020 14
Function and parameter
declaration (cont.)
User-defined function :
⚫ return statement :
⚫ Once the function computes the value, the function
returns the value via the return statement
⚫ The syntax of the return statement is:
June 4, 2020 16
Function and parameter
declaration (cont.)
User-defined function :
⚫ Consider Program 6.1.
This program will call the function AddNum( ) which
receives data from the main().
June 4, 2020 18
Function and parameter
declaration (cont.)
Function Prototype:
⚫ function heading without the body of the function
⚫ Syntax:
June 4, 2020 19
Function and parameter
declaration (cont.)
⚫ Function prototype
⚫ For example,
June 4, 2020 20
Function and parameter
declaration (cont.)
June 4, 2020 21
Function and parameter
declaration (cont.)
June 4, 2020 22
Function and parameter
declaration (cont.)
Flow of execution :
⚫ Execution always begins at
⚫ The first statement in the function main no matter
where main is placed in the program
June 4, 2020 23
Function and parameter
declaration (cont.)
Exercise 3 :
Develop a program to show the output below using
user-defined functions :
June 4, 2020 24