Also consider 3rd version of function which extends the tripleByReferfence that
ID: 3672117 • Letter: A
Question
Also consider 3rd version of function which extends the tripleByReferfence that return the reference with static scope duration variable and ensure the const on input reference parameter.
Pass-by-Value vs. Pass-by-Reference) Write a complete C+ program with the two alternate functions specified below, each of which simply triples the variable count defined in main- Then function tripleByValue that passes a copy of count by value, triples the copy and re- b) function tripleByReference that passes count by reference via a reference parameter compare and contrast the two approaches. These two functions are a) turns the new value and and triples the original value of count through its alias (i.e., the reference parameter).Explanation / Answer
#include<iostream>
using namespace std;
int TripleByValue(int count){
count = 3*count;
return count;
}
int TripleByReference(int &count){
count = 3*count;
return count;
}
int main(){
int count = 3;
//result will be triples but value of count will stay the same.
int result = TripleByValue(count);
cout<<"Result: "<<result<<endl;
cout<<"Count: "<<count<<endl;
//result and local variable count will be tripled, because values address is passed as an argument, as a result of it, if argument value is altered, then it will be refelected in local variable too because argument passed by address(also called as reference) is having same reference as local variable.
result = TripleByReference(count);
cout<<"Result: "<<result<<endl;
cout<<"Count: "<<count<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.