3

I understand why this works as it does

#include <iostream>
using namespace std;

int additionFive (int a)
{
    a = a - 5;
    return a;
}

int subtractFive (int &a)
{
    a = a -5;
    return a;
}

int main()
{
    int local_A = 10;

    cout << "Answer: " << additionFive(local_A) << endl;
    cout << "local_A Value "<< local_A << endl;

    cout << "Answer: " << subtractFive(local_A) << endl;
    cout << "local_A = Value "<< local_A << endl;

    return 0;
}

OUTPUT:

Answer: 5
local_A Value 10
Answer: 5
local_A = Value 5

But I dont understand why this change of syntax changes the answer (simply putting the arithmetic and print out on the same line)

#include <iostream>
using namespace std;

int additionFive (int a)
{
    a = a - 5;
    return a;
}

int subtractFive (int &a)
{
    a = a -5;
    return a;
}

int main()
{
    int local_A = 10;

    cout << "Answer: " << additionFive(local_A) << " local_A Value: "<< local_A << endl;
    cout << "Answer: " << subtractFive(local_A) << " local_A = Value: "<< local_A << endl;

    return 0;
}

OUTPUT:

Answer: 5 local_A Value: 10
Answer: 5 local_A = Value: 10
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
stackoverflow
  • 18,348
  • 50
  • 129
  • 196

2 Answers2

5

You're running into undefined behavior. The second version modifies the value of a which you're reading in the second cout 2 times, with no sequence points in between the reads.

First version:

cout << "Answer: " << subtractFive(local_A) << endl;
//                              |                  |
//                  reads and modifies local_A     |
//                                           sequence point
cout << "local_A Value ="<< local_A << endl;
//                             |
//                       reads local_A

Second version:

cout << "Answer: " << subtractFive(local_A) << " local_A Value: "<< local_A << endl;
//                             |                                       |
//                  reads and modifies local_A                   reads local_A
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • What do I do fix this undefined behavior to correct this problem? – stackoverflow Dec 13 '12 at 19:49
  • 2
    @stackoverflow you don't do it. :) You use the first version (2 separate `cout` statements) instead of the second. – Luchian Grigore Dec 13 '12 at 19:50
  • 1
    ok then, the point of this small program was to do two different functions; one to do pass by reference and one to do pass by value. How would you do each of those correctly then with that said. My goal was to change the local variable when sent the second function. – stackoverflow Dec 13 '12 at 19:52
  • 1
    @stackoverflow you are doing them correctly, it's just the second `cout` part that is wrong. – Luchian Grigore Dec 13 '12 at 19:53
  • would this behavior said to be that the cout of the variables happens to before the operation (function call) executes? – stackoverflow Dec 13 '12 at 20:13
  • @stackoverflow I didn't understand that. The problem is that you don't know which will happen first - reading the return value of `substractFive` or `local_A`. So C++ simply disallows it. The simple rule is that it's just illegal. Don't try to make much sense out of illegal code, just don't do it. – Luchian Grigore Dec 13 '12 at 20:15
  • 1
    @stackoverflow read this - http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points – Luchian Grigore Dec 13 '12 at 20:15
0

Well the behavior of the second code is completely system/compiler depended. On Dev C++ the second code is giving the same output as first one. It depends on how the compiler builds the cout statement in program assembly...