0% found this document useful (0 votes)
22 views47 pages

Slide Set - 15

The document discusses functions in C++ including user-defined and pre-defined functions. It covers function declaration, definition, calling and passing arguments by value and reference. Examples of functions are provided.

Uploaded by

Jahanzaib Brohi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views47 pages

Slide Set - 15

The document discusses functions in C++ including user-defined and pre-defined functions. It covers function declaration, definition, calling and passing arguments by value and reference. Examples of functions are provided.

Uploaded by

Jahanzaib Brohi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

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

• Passing Arguments to a Function By Reference (LL 04)


• Program Examples (LL 04)

LL 02 = Learning Level 02 – Comprehension, LL 04 = Learning Level 04 – Analysis


Function

• 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

• A function groups a number of program statements into a unit and gives it a


name.

• A function is a group of statements that together perform a task.

• Every C++ program has at least one function, which is main( ).


Types of Functions

• There are two types of functions:


Pre-Defined Functions

• Also called as built-in functions.


• The functions which are already built inside the C++ standard library, are
called as pre-defined functions.
• You do not need to create them just call them whenever you want to use
them.
• Few of the examples of pre-defined functions are:
Pre-Defined Functions
Pre-Defined Functions
User-Defined Functions

• Also called as Programmer-Defined functions.


• The functions which are not the part of C++ standard library.
• The programmer defines the functionality of the functions by themselves.
• Because C++ does not provide every function that you will ever need, you
must learn to write your own functions.
• For example there is no any function in C++ that finds the maximum number
out of an array or swap two variables with each other.
• For these tasks you need to create your own (User-Defined) functions.
User-Defined Functions
• A function is like a box (with group of statements inside) which receives
some of the inputs, apply processing on them and gives you the output.
• A function can have zero or more inputs.
• A function can have zero or one output.
• The group of statements will define what operation, the function will
perform on the inputs.

Inputs Function Output


User-Defined Functions
• User-defined functions in C++ are classified into two categories:

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++

• In order to create a user-defined function in C++ you need to provide:

• 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.

• Function declaration is also called as Function Prototype.


Function Declaration
Function-1 Declaration:

Function Name Parameter List

Return Type
Function Declaration
Function-2 Declaration:

Function Name Parameter List

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.

• Here, the copy of the variable is passed.

• Consider the following program:


Passing Arguments to a Function By Value
Passing Arguments to a Function By Value
Passing Arguments to a Function By Reference

• When we pass the arguments to a function by reference, the actual variables


are affected.

• Here, the reference (address) of the variables is passed.

• Consider the following program:


Passing Arguments to a Function By Reference
Passing Arguments to a Function By Reference
Program Examples
Functions in C++
Program Example 01

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

You might also like