1

Hi I'm currently learning C++ and I'm trying to pass the value by reference, but I'm having issues with getting the correct output. What seems to be the problem??

void ref(int a)
{
cout << "a = " << a << endl;
a = 1;
cout << "a = " << a << endl;
} 

int main()
{
int b = 10;
cout << "b = " << b << endl;
ref(b);
cout << "b = " << b << endl;
return 0;
}
Jacky Lin
  • 11
  • 1

2 Answers2

4

Unless you put void ref(int &a){} you are not actually changing the value of a.

Harrison Ray
  • 159
  • 3
3

For pass by reference you have to use:-

void ref(int& a) {}
ravi
  • 10,994
  • 1
  • 18
  • 36