0

The pass by reference doesn't seem to work. The second line of the output should by using the reference, but instead outputs the same thing as before.

double passByValue(double, double);
double passByRef(double&, double&);

int main(){    
    double firstNumber=0, secondNumber=0;
    char quit =' ';
    while (quit != 'y'){

        cout <<"Please enter your first number: ";
        cin >> firstNumber;

        cout << "\nPlease enter your second number: ";
        cin >> secondNumber;

        cout <<firstNumber <<"\t" << secondNumber << "\t" << passByValue(firstNumber, secondNumber)<<endl;
        cout <<firstNumber <<"\t" << secondNumber << "\t" << passByRef(firstNumber ,secondNumber )<<endl;

        cout <<"Do you want to quit? y/n";
        cin >> quit;
    }
}

double passByValue(double first, double second){
    first +=5;
    second +=5;
    double sum =first + second;
    return sum;
}

double passByRef(double &a, double &b){
    a +=5;
    b +=5;
    double sum = a + b;
    return sum;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
John
  • 79
  • 1
  • 6
  • Why do you think that passing by reference would change the output in this case? (it *might* do so, but probably not for the reason you're thinking) – Benjamin Lindley Jul 30 '15 at 20:54
  • That is not the best way to check whether pass by reference "worked". – juanchopanza Jul 30 '15 at 20:56
  • The way to see the difference between passing by value and reference is to print the values of `firstNumber` and `secondNumber` **after** each call. – Barmar Jul 30 '15 at 20:57
  • You assume that the parameters to the `<<` operators are evaluated in some specific order. They are not, so you don't know if you print the values before or after the function call. – Bo Persson Jul 30 '15 at 21:01
  • @BoPersson actually they are – Slava Jul 30 '15 at 21:06
  • Please, in the future (if is possible to create short snippet of the code) use debugger before post question in stackoverflow – Konstantin Burlachenko Jul 30 '15 at 21:07
  • I do not think this is duplicate of that question, in this case order is defined – Slava Jul 30 '15 at 21:10
  • @Slava I really don't think it is defined. – juanchopanza Jul 30 '15 at 21:10
  • @juanchopanza `a.func1().func2()` you think that `func2()` can be executed before `func1()`? – Slava Jul 30 '15 at 21:12
  • This is a better duplicate http://stackoverflow.com/questions/2129230/cout-order-of-call-to-functions-it-prints – juanchopanza Jul 30 '15 at 21:13
  • 2
    @Slava: That's a completely different situation. Here's a better example: `func1(func2()).func3(func4());` -- Then ask, can `func4()` be executed before `func2()`? And the answer is yes, it can, but `func3()` cannot be executed before `func1()`. – Benjamin Lindley Jul 30 '15 at 21:16
  • @BenjaminLindley function call is a sequence pint, is it not? does that not mean that func1() call and resolving its arguments is a sequence point? – Slava Jul 30 '15 at 21:20
  • @Slava: The process of resolving a function's arguments is not part of the function call though. – Benjamin Lindley Jul 30 '15 at 21:21
  • @BenjaminLindley `at a function call (whether or not the function is inline), after the evaluation of all function arguments (if any) which takes place before execution of any expressions or statements in the function body` what evaluation of all function arguments mean? – Slava Jul 30 '15 at 21:22
  • @Slava: Evaluation of all function arguments means evaluation of each function argument expression to a single value. Why? Also, what exactly are you referencing, and if it's the standard, which version/chapter/section/paragraph? – Benjamin Lindley Jul 30 '15 at 21:29
  • @BenjaminLindley I created separate question http://stackoverflow.com/questions/31734441/order-of-evaluation-in-chain-invocation-in-c – Slava Jul 30 '15 at 21:34

2 Answers2

1

Try this:

double sum = passByValue(firstNumber, secondNumber);
cout << firstNumber <<"\t" << secondNumber << "\t" << sum <<endl;
sum = passByRef(firstNumber, secondNumber);
cout << firstNumber <<"\t" << secondNumber << "\t" << sum <<endl;

This ensures that you're seeing the changes to firstNumber and secondNumber that occurs as a result of calling the function.

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

The code is correct, your interpretation isn't.
Try displaying firstNumber and secondNumber after the call to passByReference, you should get that:

0     0     10  
0     0     10  
5     5  

The thing is, when you called passByValue, these lines

first +=5;
second +=5;

didn't actually increase firstNumber and secondNumber: firstand second were copies.
On the other hand, when you pass by reference, they aren't hence, the 5.

Caninonos
  • 1,214
  • 7
  • 12