Programming Fundamental
Lab Manual (Lab 13)
Lab Instructor: Miss Ayesha Majid
Session: Fall 2021
School of Systems and Technology
UMT Lahore Pakistan
More on Functions
Objectives
Default parameter
Pass by value and pass by reference
What is a parameter in C++:
The terms parameter and argument are sometimes used interchangeably.
However, parameter refers to the type and identifier, and arguments are the values passed to
the function. In the following C++ example, int a and int b are parameters, while 5 and 3 are
the arguments passed to the function.
Syntax
void functionName(parameter1, parameter2, parameter3) {
// code to be executed
}
Default parameters
A default parameter is a function parameter that has a default value provided to it. If the user does not
supply a value for this parameter, the default value will be used. If the user does supply a value for the
default parameter, the user-supplied value is used.
Sample Task:
#include<iostream>
using namespace std;
void PrintValues(int nValue1, int nValue2=10)
{
cout<<"1st value: " << nValue1 <<endl;
cout<<"2nd value: " << nValue2 <<endl;
}
int main()
{
PrintValues(1); // nValue2 will use default parameter of 10
PrintValues(3, 4); // override default value for nValue2
}
Output:
Multiple Parameters of different data types:
You can add as many parameters as you want:
Example:
#include <iostream>
using namespace std;
void myFunction(string fname, int age) {
cout << fname << " Refsnes. " << age << " years old. \n";
}
int main() {
myFunction("Liam", 3);
myFunction("Jenny", 14);
myFunction("Anja", 30);
return 0;
}
Arguments passed by value and by reference
Arguments Passed by Value:
In the functions seen earlier, arguments have always been passed by value. This means that, when calling
a function, what is passed to the function are the values of these arguments on the moment of the call,
which are copied into the variables represented by the function parameters. For example, take:
1 int x=5, y=3, z;
2 z = addition ( x, y );
In this case, function addition is passed 5 and 3, which are copies of the values of x and y, respectively.
These values (5 and 3) are used to initialize the variables set as parameters in the function's definition, but
any modification of these variables within the function has no effect on the values of the variables x and y
outside it, because x and y were themselves not passed to the function on the call, but only copies of their
values at that moment.
Arguments Passed by Reference:
Example:
#include <iostream>
using namespace std;
void swapNums(int &x, int &y) {
int z = x;
x = y;
y = z;
}
int main() {
int firstNum = 10;
int secondNum = 20;
cout << "Before swap: " << "\n";
cout << firstNum<<endl << secondNum << "\n";
swapNums(firstNum, secondNum);
cout << "After swap: " << "\n";
cout << firstNum<<endl << secondNum << "\n";
return 0;
}
Output:
Lab Tasks
Task 1
Given three variables x, y, z. Write a function to circularly shift their values to right using pass by
reference. In other words, if x = 5, y = 8, z = 10 after circular shift y = 5, z = 8, x =10 after circular shift y
= 5, z = 8 and x = 10. Call the function with variables a, b, c to circularly shift values.
.
Task 2
Find out the area and circumference of circle using pass by reference within function.
Task 3:
Find the factorial of number entered by user by passing arguments by reference.