2016-02-04 89 views
-4

我不知道爲什麼結果是這樣的?我預計15 50 5 任何人可以幫助我瞭解的結果我不知道爲什麼結果是這樣的?我預計15 50 5

#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? 
+1

結果是像什麼? –

+1

我不知道你得到了什麼,但預計是:15 50 10 –

+0

vs 2015給我的結果是15 10 5. – Kasra

回答

0
  • 只需調用你的函數是這樣的:

    int a = f(x, y); 
    cout << a << " " << x << " " << y; 
    
+0

amazing.it works.thank你很多。但是什麼原因? – Kasra

+1

我不能發表一個答案,所以沒有完整的代碼對不起,但如果你檢查反彙編,首先Y得到饋送到流,然後,X得到喂,然後它調用f,並提供結果。 http://s30.postimg.org/8ke0jt08h/disass.jpg 這是在VS2015 –

+0

,因爲評估順序未指定。可能是從左到右或從右到左。 –

相關問題