Write your question here.
Task#1: Practice the concept of Void Functions by showing the output of the
following C++ Programs including the function Modify to discover how the Value
and Reference parameters work, then write a short sentence to explain the difference
between them in the achieved results:
Note: put the screen shot of the output under each program below
a) //Function Modify with (Value parameters)
#include<iostream>
usingnamespace std;
void modify(int m); // function prototype
int main()
{
int x;
x=7;
cout<<"x befor calling modify function = "<<x<<endl;
modify( x ) ; //calling statement x is actual parameter
cout<<"x After calling modify function = "<<x<<endl;
cout << endl;
return 0;
}
// Function definition
void modify( int y ) // Heading y is Formal Parameter
{
y=y+10;
cout<<"y inside modify function = "<<y<<endl;
}
b) // Function Modify with (Reference parameters)
#include<iostream>
usingnamespace std;
void modify( int &m ); // function prototype
int main()
{
int x;
x=7;
cout<<"x befor calling modify function = "<<x<<endl;
modify( x ) ; //calling statement x is actual parameter
cout<<"x After calling modify function = "<<x<<endl;
cout << endl;
return 0;
}
// Function definition
void modify( int &y ) // Heading y is Formal Parameter
{
y=y+10;
cout<<"y inside modify function = "<<y<<endl;
}
First quit writing your code to benefit the compiler. You have failed in this attempt. To much white space.
Write your code so that you can read and follow it.
Give you variables a good name. Not a single letter that has no meaning.
Prefer to use the new line, (\n), over the function "endl".
It may be legal to change a variable name in a function parameter, but it does not mean that you have to. Most times using the same variable name that is sent to the function is best for readability and folling the variable.
// a) //Function Modify with (Value parameters)
#include<iostream>
usingnamespace std;
void modify(int m); // function prototype
int main()
{
//int number;
//number = 7;
int number{ 7 }; // <--- You can define the variable and initialize at the same time.
cout << "\n Number before calling modify function = " << number << '\n';
modify(number); //calling statement number is actual parameter
cout << "\n Number After calling modify function = " << number << '\n';
cout << '\n';
return 0; // <--- Not required, but makes a good break point for testing.
}
// Function definition
void modify(int number) // Heading number is Formal Parameter
{
number = number + 10;
cout << "\n Number inside modify function = " << number << '\n';
}
Both pieces of code work, what do you not understand?
The first is pass by value and the second is pass by reference.
By value is only a copy of the variable. You can use it change it, but when the function ends so does the variable because it is local to the function. So any changes made are lost when the function ends.
Pass by reference is not a copy, but used the variable that was sent to the function, so any changes are reflected back where the variable was defined and passed to the function.
By reference still works when a function passes a passed variable to another function.
Andy
P.S. if you understand the difference of pass by value and pass by reference the answer is right there.