-
a simple C++ question
Anyone know what means if got a '&' after the variable
in the function???
when i read the book , it's said pass the variable by reference, but i still not understand that!!
so anyone can explain to me and give me some example??
Thanks............................
-
an & before a variable suggests its actual address.
call by reference means that if any change in value is done to the variable passed in the functon then it will be a permanent change.
eg
void func1(int &a, int b)
{
a=a+1;
b=b+1;
}
now if in main() you display value of a it will be +1 but b will remain as the original val.
-
int main()
{
int A, B, C;
A=2;
B=4;
C=6;
mystery(B,C,A);
cout<<A+B+C;
}
void mystery(int D,int& E, int C)
{
int A;
A=C+1;
E=A+C;
C=C*2;
}
so is that A=2 (unchange)
B=4 (unchange)
C=5 (changed)
and the output is 11
is that correct??
i just found the question from my tutorial!!
Thanks a lot