Lab Manual 13
Lab Manual 13
Default parameter
Pass by value and pass by reference
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;
int main()
{
PrintValues(1); // nValue2 will use default parameter of 10
PrintValues(3, 4); // override default value for nValue2
}
Output:
Example:
#include <iostream>
using namespace std;
int main() {
myFunction("Liam", 3);
myFunction("Jenny", 14);
myFunction("Anja", 30);
return 0;
}
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.
int main() {
int firstNum = 10;
int secondNum = 20;
swapNums(firstNum, secondNum);
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: