Follow up question re Calling by Value vs Reference
Hi All, another easy one for you guys...
I'm trying to figure out why I'm receiving the same results after executing both functions in the following program. Shouldn't the bolded line produce a result of 15?
Thanks for any help...
_________________
int tripCBV(int);
int tripCBR(int &);
int main()
{
int count = 5;
cout << "BEFORE - CBV result is " << count << endl;
cout << "AFTER - CBV result is " << tripCBV(count) << endl;
cout << "AFTER function result is " << count << endl << endl;
cout << "BEFORE - CBR result is " << count << endl;
cout << "AFTER - CBR result is " << tripCBR(count) << endl;
cout << "AFTER function result is " << count << endl << endl;
return 0;
}
int tripCBV(int x)
{
int y;
y = 3 * x;
return y;
}
int tripCBR(int &x)
{
int y;
y = 3 * x;
return y;
}
cout << "AFTER function result is " << count << endl << endl;
Re: I still don't see it...
>>You both mention I never changed the value... isn't that what I was doing within the functions?
Look at this:
Code:
int tripCBR(int &x)
{
int y;
y = 3 * x;
return y;
}
When do you assign a new value to x? You don't is the answer. ;)
>>I also changed my return statement to 'return 3 * x;'
This also doesn't change x.
Do something like
x *= 3;
which is shorthand for
x = x * 3;