Sinan
Sinan

Reputation: 463

C++ references and references parameters

Please look at the code snippet below - I have declared 3 functions (i.e. 1 passing an int and the others passing reference to an int). After executing the program I found that the value of "count" variable after calling function (tripleByReference) has not been changed to reflect its triple (count is still equal to 5). However calling function (tripleByReferenceVoid) modifies the variable but this is due to the fact that changes occurred directly to the variable (count).

I understand that with pass by reference, the caller gives the called function the ability to access the caller's data directly and modify it but that could not be achieved by passing the variable to function (tripleByReference) - Please help me to understand this.

#include <iostream>
using namespace std;

/* function Prototypes */
int tripleByValue(int);
int tripleByReference(int &);
void tripleByReferenceVoid(int &);

int main(void)
{
    int count = 5;

    //call by value
    cout << "Value of count before passing by value is: " << count << endl;
    cout << "Passing " << count << " by value, output is: "
         << tripleByValue(count) << endl;
    cout << "Value of count after passing by value is: " << count << endl;

    //call by reference - using int tripleByReference
    cout << "\n\nValue of count before passing by reference is: " << count << endl;
    cout << "Passing " << count << " by reference, output is: " 
         << tripleByReference(count) << endl;
    cout << "Value of count after passing by reference is: " << count << endl;

     //call by reference - using void tripleByReference
     tripleByReferenceVoid(count);
     cout << "\n\nValue of count after passing by reference is: " << count << endl;
    cout << endl;
    system("PAUSE");
    return 0;
}//end main

int tripleByValue (int count) {
    int result = count * count * count;
    return result; 
}//end tirpleByValue function

int tripleByReference(int &count) {
     int result = count * count * count;
     return result; //perform calcs     
}//end tripleByReference function

void tripleByReferenceVoid(int &count) {
     count *= count * count;
}//end tripleByReference function

Thank you.

Upvotes: 0

Views: 491

Answers (2)

AnT stands with Russia
AnT stands with Russia

Reputation: 320491

In your function tripleByReference you are not even attempting to modify the value of count, while in your function tripleByReferenceVoid you are explicitly modifying count. Hence the obvious and expected effect: the latter function modifies count, the former doesn't. I.e. these functions do exactly what and only what you explicitly and consciously asked them to do.

Questions like this one is difficult to answer because it is virtually impossible to understand what made you to ask it. You seem to be puzzled by the fact that the behavior of these two functions is different. But why does it puzzle you, when you yourself explicitly wrote them to be different in that specific regard?

Upvotes: 3

Marlon
Marlon

Reputation: 20312

tripleByReference doesn't change the value of count because you never assign to it. You are returning the value instead.

tripleByReferenceVoid is different. You are assigning to it (count *= ...) and that is why these changes are reflected.

Upvotes: 4

Related Questions