Slide Set - 15
Slide Set - 15
CP & CPSA
17ME & 17PG
Contents
• Function (LL 02)
• Types of Functions (LL 02)
• Pre-Defined Functions (LL 04)
• User-Defined Functions (LL 04)
• Creating User-Defined Functions in C++ (LL 04)
• Function Declaration (LL 04)
• Function Definition (LL 04)
• Function Calling (LL 04)
• Passing Arguments to a Function (LL 04)
• Passing Arguments to a Function By Value (LL 04)
LL 02 = Learning Level 02 – Comprehension, LL 04 = Learning Level 04 – Analysis
Contents
• Most computer programs that solve real-world problems are much larger
than the programs we have done in the class.
• Experience has shown that the best way to develop and maintain a large
program is to construct it from small, simple pieces, or components.
• This technique is called divide and conquer.
• A larger program is created by developing smaller components individually
and them assembling them together as whole.
Function
1. Value-Returning Functions
Functions that have a return type. These functions return a value of a specific
data type using the return statement.
2. Void Functions
Functions that do not have a return type. These functions do not use a return
statement to return a value.
User-Defined Functions
• In first case the function takes some inputs, performs calculations and
returns one value as a result. Lets say you want to create a function that
receives the radius of the circle and returns you the area of the circle, in this
case the function returns one value i.e. the area, hence it is a value returning
function.
• In second case the function just does its job but do not return a value. Say,
we need to create a function that receives two strings and displays both the
strings after concatenating. In this case the function does not perform any
calculation and will not return any value.
Creating User-Defined Functions in C++
• Function Declaration
• Function Definition
• Function Calling
Creating User-Defined Functions in C++
• Lets create two functions:
Function 1:
Create a function that receives the radius of the circle and returns you the area
of the circle.
Functions 2:
Create a function that receives two strings and displays both the strings after
concatenating.
Function Declaration
• The function declaration just tells the compiler how the function looks like.
It includes the function name, the parameter list and the return type.
• The function declaration just tells the compiler that, we are going to create
one of the function in this program and it looks like this.
Return Type
Function Declaration
Function-2 Declaration:
Return Type
Function Definition
• The function definition provides the details of the functions. Here we write
all the statements that make up a function.
• Function definition tells the compiler, what the function will do.
• It includes, the function name, return type, parameter list and the body
of the function.
Function Definition
Function-1 Definition: Parameter List
Function Name
Return Type
Function Body
Function Definition
Function-2 Definition:
Parameter List
Function Name
Return Type
Function Body
Function Calling
• Once the function is created, it can be used inside the program by calling it.
• It includes, the function name and argument list.
• Arguments are different then parameters.
• Parameters are the variables that we use while function definition.
• Arguments are the values/variables that we use while calling the function.
Function Calling
Function-1 Calling:
Argument List
Function Name
Function Calling
Function-2 Calling:
Argument List
Function Name
Passing Arguments to a Function
• While calling the function, we have to specify all the arguments of the
function for each of the parameter.
• In a function the arguments can be passed “By Value” or “By Reference”.
Passing Arguments to a Function By Value
• When we pass the arguments to a function by value, the actual variables are
not affected.
Problem Statement:
Create a function that receives two integer numbers and returns
the maximum number out of them.
Program Example 01
Program Example 01
Program Example 02
Problem Statement:
Create a function that receives two integer numbers and swaps
them.
Program Example 02
Program Example 02
Program Example 03
Problem Statement:
Create a function that receives two floating point number and an
operator (+ , -, / , * ). The function returns the result of the
operation.
Program Example 03
Program Example 03
Program Example 04
Problem Statement:
Create two functions, first receives temperature in Fahrenheit and
converts it in to Celsius. The second receives temperature in Celsius
and converts it in to Fahrenheit
Program Example 04
Program Example 04
Program Example 05
Problem Statement:
Create two functions, first receives integer array and returns
maximum item. The second receives integer array and returns
minimum item.
Program Example 05
Program Example 05