I couldn't understand how the value of m=7 after execution of program.
void test(int m , int &n)
{
n=n-2*m;
m=2*m;
}
int main()
{
int a=7, b=12;
test(a,b);
cout << a<<b;
return 0;
}
CodePudding user response:
The 'm' is a local variable.So in the function'test',it is '14' right.If you want change the 'a',you should use "int&m".
CodePudding user response:
If you are wondering why cout << a<<b is printing 7 for first value, it is because you did not pass first parameter to function test as reference. So any change done on the first parameter in the test function is local to the test function only and the change would not be reflected in the main function. If you pass first parameter also as reference (as you did for the second one) you would see different output.