CC103 Mod2
CC103 Mod2
CC103 Mod2
0 10-July-2020
From the first module, you have realized the importance of using functions in your programs. We will have to
discuss now how parameters are being passed to the arguments.
Introduction
Parameters can be passed via two ways, (1) by value and (2) by reference. In this unit, we will be discussing
the differences between the two.
int main()
{
int x{45}, y{12};
cout<<"Before function call: \n";
cout<<"x: "<<x<<endl;
cout<<"y: "<<y<<endl;
addOne(x, y); //x, y are arguments
cout<<"After function call: \n";
cout<<"x: "<<x<<endl;
cout<<"y: "<<y<<endl;
return 0;
}
[Sample Output]
Before function call:
x: 45
y: 12
After function call:
x: 45
y: 12
After the function, the values of the arguments are passed to the parameters.
Hence, the parameters a and b are initialized with 45 and 12, respectively. The arguments x and y retain their
values. Within the function, the value of a and b are increased by one (1). However, the values of x and y are
not changed. That explains the output of 45 and 12 for x and y, even after calling the addOne() function.
Remember that when using passing by value, arguments are never changed by the function being called.
Use parameter passing by reference if you want to access a variable from within a function. To pass a
variable by reference, declare the parameter as references like you can see in the next example:
C++ Code [function7.cpp]
#include<iostream>
using namespace std;
int main()
{
int x{45}, y{12};
cout<<"Before function call: \n";
cout<<"x: "<<x<<endl;
cout<<"y: "<<y<<endl;
addOne(x, y);
cout<<"After function call: \n";
cout<<"x: "<<x<<endl;
cout<<"y: "<<y<<endl;
return 0;
}
[Sample Output]
Before function call:
x: 45
y: 12
After function call:
x: 46
y: 13
The only difference between this code from the previous one is the function signature
void addOne(int &a, int &b). The parameters are declared as references using the &.
Notice how the behavior of passing by reference.
The parameters a and b are references to the variables x and y; such that the values of x and y are also the
values of a and b. However within the function, the values of a and b are increased by 1, therefore, being
references to x and y, the values of x and y are the ones increased. When we displayed the values of x and y
in the main function after the function call, the increased values are displayed.
Remember that references allow a function to change the value of the argument.
LEARNING ACTIVITY 2
1. Write a program that displays an average of three integers. The program should pass the integers to
a user-defined function named getAverage, which should return the average as a double number. The
function parameters should be named as num1, num2, and num3. Use a function prototype.
2. Write a function that takes two integers as arguments and returns the value of the larger one.
3. Write a function that takes an integer as argument and returns boolean true if it is prime, and false if
otherwise.
SUMMARY
User-defined functions make your code reusable as you can declare them once and use them multiple times.
Further, your codes divided into functions can be managed more easily and increases code readability.
Hence, the use of user-defined functions is encouraged. Parameters for functions can be passed by either
value or reference. If passing by value, the value of the argument is passed to the parameter, acting as
another copy of the argument. Here, the argument is not affected if the value of the parameter is changed. In
passing by reference, the reference to the argument is passed to the parameter. Hence, when we change the
value of the parameter, the argument is changed accordingly.
REFERENCES