Functions in C++ by SLIIT
Functions in C++ by SLIIT
Functions in C++
4
IT1050| Object Oriented Concepts| Functions in C++|Anjalie G
Example-01 int findSum(int x, int y)
{
return ( x + y );
#include <iostream> }
using namespace std;
num2
int main() num1
x y
{
int num1, num2,sum;
sum
cout<<“Input two numbers :”;
x+y
cin >> num1 >> num2;
sum = findSum(num1, num2 );
cout<<“Sum is : “<<sum <<endl;
return 0;
} //main
distance Time
MPH Elapsed Time
6
IT1050| Object Oriented Concepts| Functions in C++|Anjalie G
Implementing a function
Function Heading
return_type function_name(parameter_list)
Eg : return avg;
Eg : return ( num1+num2) /2.0f;
7
IT1050| Object Oriented Concepts| Functions in C++|Anjalie G
ElapsedTime() function
float elapsedTime(int Distance, float MPH)
{
if(MPH > 0.0)
return(Distance/MPH);
else
cout << “MPH is not greater than 0!”;
return 0.0;
}
8
IT1050| Object Oriented Concepts| Functions in C++|Anjalie G
Invoking Functions
• Functions can be invoked by using their name
followed by the argument list, which is enclosed in
parentheses, anywhere a value of the same type as the
return type can be used.
Eg : avg = calculateAvg( 10, 20 );
return 0;
} //main
10
IT1050| Object Oriented Concepts| Functions in C++|Anjalie G
Invoking ElapsedTime()
Mile Speed
Time = elapsedTime(Mile, Speed);
283 52.5
11
IT1050| Object Oriented Concepts| Functions in C++|Anjalie G
Sample output for Example-02
Please type the distance in miles: 283
Please type the speed as miles per hour: 52.5
The elapsed time for a trip of 283 miles,
at a rate of 52.5 MPH is 5.39 hours.
12
IT1050| Object Oriented Concepts| Functions in C++|Anjalie G
Arguments and Parameters
• The term argument refers to the values passed into
the function. The arguments appear in the invocation
of the function.
Time = elapsedTime(Mile, Speed);
13
IT1050| Object Oriented Concepts| Functions in C++|Anjalie G
Arguments and Parameters
• The first argument matches with the first
parameter, the second argument matches the
second parameter, and so forth.
• The argument and the parameter do not have to
have the same name.
• The argument may be a constant, a variable, or an
expression, when using pass by value.
• The parameter must be a variable.
14
IT1050| Object Oriented Concepts| Functions in C++|Anjalie G
Parameter Passing - Pass by Value
• Pass by value is a method of passing
information to a function whereby the
parameter receives a copy of the value of the
argument.
• Any changes that the function makes to the
parameter when pass by value is used are
made to the copy and not to the original
argument.
15
IT1050| Object Oriented Concepts| Functions in C++|Anjalie G
Example-03 - Pass by Value
void change(int X);
main
int main()
{ int I=5; I 5
cout << “First time I is ”<< I <<endl;
change(I);
cout << “Next time I is ”<< I << endl;
} //main
Output
void change(int X) First time I is 5
{ cout << “Entering function X is ”<< X;
X = 7;
cout << “Leaving function X is ”<< X;
} //change
16
IT1050| Object Oriented Concepts| Functions in C++|Anjalie G
Example-03
void change(int X);
void change(int X)
Output
{ cout << “Entering function X is ”<< X;
X = 7; First time I is 5
cout << “Leaving function X is ”<< X;
Entering function X is 5
} //change
17
IT1050| Object Oriented Concepts| Functions in C++|Anjalie G
Example-03
void change(int X);
void change(int X)
Output
{ cout << “Entering function X is ”<< X;
X = 7; First time I is 5
cout << “Leaving function X is ”<< X;
Entering function X is 5
} //change
Leaving function X is 7
18
IT1050| Object Oriented Concepts| Functions in C++|Anjalie G
Example-03
void change(int X);
19
IT1050| Object Oriented Concepts| Functions in C++|Anjalie G
Parameter Passing – Pass By Reference
• Pass by reference or call by reference is the
technique of making a variable accessible to a
function by passing its address. Because the
function has direct access to the argument,
through its address, the function may modify
the argument.
• In C++, pass by reference is obtained by placing
an & immediately following the data type for
the parameter.
20
IT1050| Object Oriented Concepts| Functions in C++|Anjalie G
Example-04 - Pass by Reference
void change(int &X);
main
5 7
int main() I
{ int I=5;
cout << “First time I is ”<< I <<endl;
change(I);
cout << “Next time I is ”<< I << endl; change
} //main
X 5 7
21
IT1050| Object Oriented Concepts| Functions in C++|Anjalie G
When To Use Pass By Reference
• Use pass by reference whenever the function returns
a value or multiple values through the parameter
list.
• Use pass by reference for two way communication,
i.e. the function modifies a parameter.
• Use pass by reference to reduce storage needs when
passing large objects.
• Input and Output streams must be passed to a
function by reference.
22
IT1050| Object Oriented Concepts| Functions in C++|Anjalie G
Example-05
• Write a program to input two numbers
from the keyboard and find the sum and
average by using a function.
return 0;
}
findSumAvg() Parameters
x y sum avg
5 4
findSumAvg() Parameters
x y sum avg
5 4 9 4.5
Chapter 06
Deitel & Deitel’s (2016), C++ How to Program,
9th Edition