なぜこれが機能するのか理解しています
#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