Вы находитесь на странице: 1из 24

Function

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

⚫ Like miniature programs

⚫ Can be put together to form a larger program

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) :

⚫ Function pow has two parameters which returns a


value of type double

⚫ x and y are called the parameters (or arguments) of


the function pow

June 4, 2020 7
Function and parameter
declaration (cont.)
Pre-defined function :

⚫ The square root function sqrt(x):

⚫ Calculates the non-negative square root of x

⚫ Return a value of type double

⚫ Has only one parameter

June 4, 2020 8
Function and parameter
declaration (cont.)
Pre-defined function :

⚫ The floor function floor(x):

⚫ Calculates largest whole number not greater than x

⚫ floor(48.79) is 48.0

⚫ Return value of type double

⚫ Has only one parameter

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

⚫ Value-returning functions: have a data type

⚫ To use these functions you need to:

⚫ Include the correct header file


⚫ Know the name of the function
⚫ Know the number of parameters, if any
⚫ Know the data type of each parameter
⚫ Know the data type of the value computed by
June 4, 2020
the function, called the type of the function 11
Function and parameter
declaration (cont.)
User-defined function :
⚫ Properties that form the function definition:
1. Name of the function
2. Number of parameters
3. Data type of each parameter
4. Type of the function
5. Code required to accomplish the task (the
body of the function)

June 4, 2020 12
Function and parameter
declaration (cont.)
User-defined function :
⚫ Syntax:

⚫ functionType : type of the value returned by the


function (data type)
⚫ The syntax for a function call is:

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()

⚫ A call to a value-returning function with an empty


formal parameter list is:

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:

⚫ When a return statement executes


⚫ Function immediately terminates
⚫ Control goes back to the caller
June 4, 2020 15
Function and parameter
declaration (cont.)
User-defined function :
⚫ Example :

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().

⚫ Two integers will be passed to the AddNum() function


and the function will return the value of sum.

⚫ This is declared by the function prototype. The


function AddNum() is called in the last statement in
the program.
June 4, 2020 17
Example 1:
6.2
Function and parameter
declaration (cont.)

June 4, 2020 18
Function and parameter
declaration (cont.)
Function Prototype:
⚫ function heading without the body of the function
⚫ Syntax:

⚫ It is not necessary to specify the variable name in the


parameter list
⚫ The data type of each parameter must be specified

June 4, 2020 19
Function and parameter
declaration (cont.)
⚫ Function prototype
⚫ For example,

double AddNum(int x, int y);


double AddNum(int, int );

⚫ There must be a semi-colon at the end of each function


prototype or the compiler will think that the prototype is
the actual definition of the function

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

⚫ Other functions are executed only when they are called

⚫ Function prototypes appear before any function definition


⚫ The compiler translates these first

⚫ The compiler can then correctly translate a function call

June 4, 2020 23
Function and parameter
declaration (cont.)
Exercise 3 :
Develop a program to show the output below using
user-defined functions :

Enter first number >>


Enter second number >>
The sum of the numbers >>
The subtraction of the numbers >>
The product of the numbers >>

June 4, 2020 24

Вам также может понравиться