CC103 Mod2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

FM-AA-CIA-15 Rev.

0 10-July-2020

Study Guide in CC 103 Intermediate Programming Module No. 2

STUDY GUIDE FOR MODULE NO. 2

FUNCTION PARAMETER PASSING MECHANISMS


MODULE OVERVIEW

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.

MODULE LEARNING OBJECTIVES

By the end of this module you should be able to:


 Differentiate and implement the proper use of parameter passing

LEARNING CONTENTS (PARAMETER PASSING MECHANISMS)

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.

2.1 Passing by Value


The use of parameters in all of our previous examples uses parameter passing by value. By this, the
argument’s value is copied into the value of the corresponding function parameter. For example, let’s examine
the next code:

C++ Code [function6.cpp]


#include<iostream>
using namespace std;

void addOne(int a, int b) //a and b are function parameters


{
a+=1;
b+=1;
}

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;

PANGASINAN STATE UNIVERSITY 1


FM-AA-CIA-15 Rev. 0 10-July-2020

Study Guide in CC 103 Intermediate Programming Module No. 2

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.

2.2 Passing by Reference

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;

void addOne(int &a, int &b)


{
a+=1;
b+=1;
}

int main()
{
int x{45}, y{12};
cout<<"Before function call: \n";
cout<<"x: "<<x<<endl;
cout<<"y: "<<y<<endl;

PANGASINAN STATE UNIVERSITY 2


FM-AA-CIA-15 Rev. 0 10-July-2020

Study Guide in CC 103 Intermediate Programming Module No. 2

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.

PANGASINAN STATE UNIVERSITY 3


FM-AA-CIA-15 Rev. 0 10-July-2020

Study Guide in CC 103 Intermediate Programming Module No. 2

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

Zak, D. (2016). An Introduction to Programming with C++ (8E), pages 291-341

Online Reading Materials:


 https://www.programiz.com/cpp-programming/function-overloading
 https://www.learncpp.com/cpp-tutorial/71-function-parameters-and-arguments/
 https://www.learncpp.com/cpp-tutorial/72-passing-arguments-by-value/
 https://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/

PANGASINAN STATE UNIVERSITY 4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy