I am just starting c++ and having a slight issue understanding a piece of code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<iostream>
usingnamespace std;
void Add25(int &b)
{
b = b + 25;
}
int main()
{
int a = 10;
Add25(a);
cout << a << endl;
system("pause");
return 0;
}
Why is it cout << a and not b. since b is actually the calculation and a is just being used for a value?
Yes that is right. He doesn't really understand the "&" part
I'm glad you can read minds and deign to share it with us mundane folk.
b is a variable which exists only in Add25 which is the reason it can't be accessed in main. If b were not a reference it still couldn't be accessed in main. b being a reference doesn't have any effect on where it may accessed. That is determined solely by the scope of the variable.