The usual concept of passing a variable by reference to a function is that you want to use that exact variable and not its copy, which is fine. But suppose you have this code:
#include <iostream>
using namespace std;
int dev(int &a, int b)
{
a = 5 + b;
return b;
}
int main()
{
int a = 0;
int b;
cin >> a >> b;
cout << dev(a, b) << " "<< a;
return 0;
}
for inputs a = 5, b = 10
I am expecting to output 10 15, but I get 10 5.
Now the problem goes away if instead of
cout << dev(a, b) << " "<< a;
I write
int res = dev(a,b);
cout << res << " " << a;
I was wondering why it would be so?
Edit: spelling