-4

I don't know why the result is like this? I expected 15 50 5. Could anybody help me understand the result

#include<iostream>
#include<conio.h>

using namespace std;

int f(int &a, int &b)
{
    int m = a;
    int n = b;
    a = a*b;
    b = a / b;
    return n + m;
}

int main()
{
    int x = 10;
    int y = 5;
    cout << f(x, y) << " " << x << " " << y;
    _getch();
    return 0;
}
//why is the result like this?
Frederik Struck-Schøning
  • 11,909
  • 7
  • 55
  • 63
Kasra
  • 63
  • 7

1 Answers1

0
  • Just call your function like this:

    int a = f(x, y);
    cout << a << " " << x << " " << y;
    
Sagar Patel
  • 794
  • 1
  • 9
  • 22
  • amazing.it works.thank you a lot.but what is the reason??? – Kasra Feb 04 '16 at 09:38
  • 1
    I cannot post an answer anymore, so no full code sorry, but if you check the disassembly , first Y gets fed to the stream, then , X gets fed then it calls f, and feeds the result. http://s30.postimg.org/8ke0jt08h/disass.jpg This is on VS2015 – Marco Giordano Feb 04 '16 at 09:41
  • because order of evaluation is unspecified.May be from left-to-right or right-to-left. – Sagar Patel Feb 04 '16 at 09:41
  • i think you're right.that was really kind of you – Kasra Feb 04 '16 at 09:44