IN C++ Write a simple program to validate passing by value and passing by refere
ID: 674650 • Letter: I
Question
IN C++
Write a simple program to validate passing by value and passing by reference.
Initialize a variable and display its value.
Pass the variable by value to a function. Change the value of the variable in the pass by value function. Display the modified value inside the function.
After the pass by value function returned, display the value again in the main function.
Pass the same variable, now passing by reference to another function. Change the value of the variable in the pass by reference function. Display the modified value inside the function.
After the pass by reference function returned, display the value again in the main function.
See the sample code below for an example of how your program output should look:
Variable name has the value "Barry"
calling pass by value
Parameter now has the value "Thomas"
pass by value returned
Variable name has the value "Barry"
calling pass by references
Parameter now has the value "James"
call by reference returned
Variable name has the value "James"
Name the project: PassingByValueReference.cpp
Program #2 Instructions:
Write another simple Address program to print the value of the memory addresses of the variables listed below:
a char
a string
an array of integers
an integer
a double
See the sample code below for an example of how your program output should look:
Integer has memory address 0x434232
String has memory address 0x434234
Array has memory address 0x434320
Array[1] has memory address 0x434322
Array[2] has memory address 0x434324
Array[3] has memory address 0x434326
etc...
etc...
Variable name has the value "Barry"
calling pass by value
Parameter now has the value "Thomas"
pass by value returned
Variable name has the value "Barry"
calling pass by references
Parameter now has the value "James"
call by reference returned
Variable name has the value "James"
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
void value(string name){
name="Thomas";
cout<<"Parameter name has the value ""<<name<<"""<<endl;
}
void ref(std::string& name) {
name="James";
cout<<"Parameter name has the value ""<<name<<"""<<endl;
}
int main() {
string name="Barry";
cout<<"Variable name has the value ""<<name<<"""<<endl;
cout<<"calling pass by value"<<endl;
value(name);
cout<<"pass by value returned"<<endl;
cout<<"Variable name has the value ""<<name<<"""<<endl;
cout<<"calling pass by reference"<<endl;
ref(name);
cout<<"call by reference returned"<<endl;
cout<<"Variable now has the value ""<<name<<"""<<endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
int i;
cout<<"Integer has memory address "<<&i<<endl;
char c;
cout<<"character has memory address "<<&c<<endl;
int arr[3];
cout<<"Array has memory address "<<&arr<<endl;
cout<<"Array[1] has memory address "<<&arr[1]<<endl;
cout<<"Array[2] has memory address "<<&arr[2]<<endl;
cout<<"Array[3] has memory address "<<&arr[3]<<endl;
double d;
cout<<"double has memory address "<<&d<<endl;
string s;
cout<<"string has memory address "<<&s<<endl;
return 0;
}