我明白为什么会这样
#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;
}
输出:
Answer: 5
local_A Value 10
Answer: 5
local_A = Value 5
但我不明白为什么这种语法变化会改变答案(只需将算术和打印在同一行)
#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;
}
输出:
Answer: 5 local_A Value: 10
Answer: 5 local_A = Value: 10