0

I found interesting example of code and don't understand, why output is 656. I thought Function had to been called first, so variable 'b' had to been changed second and output will be 655. But this example shows reversing calling. Why? I tried to separate 'cout' in three parts and everything works as I guessed.

#include <iostream>

int Function(int number1, int& number2)
{
    number1++;
    number2--;
    return number1;
}

int main()
{
    int a = 5, b = 6;
    std::cout << Function(a, b) << a << b;  // 656

    return 0;
}
  • Notice: Pass by value, pass by reference, return value. – Quimby Sep 24 '20 at 08:38
  • I tried it with gcc 7.4.0, and I got 655, not 656 – Damien Sep 24 '20 at 08:41
  • @Damien that you get `655` in your test does not mean, that `656` won't happen or would not be an expected result. – t.niese Sep 24 '20 at 08:43
  • @t.niese I just wanted to emphasize that the result is compiler dependent. It is why I detailed which compiler I used. I should have been more clear effectively. – Damien Sep 24 '20 at 08:50
  • I found similar question here, and this is the Stroustrup's answer: The order of evaluation of subexpressions within an expression is undefined. So that really depends of what kind of compiler you use. – Demien Blogan Sep 24 '20 at 08:56

0 Answers0