I'm confused with references syntax

I'm in a C++ 1 class at the local community college. I'm confused with the syntax of using references. I know the syntax. I don't know how it works. For instance, this is directly from our textbook:

prototype:
 
void AskForXandY(int &rX, int &rY);


function body:
1
2
3
4
5
void AskForXandY(int &rX, int &rY)
{
   cout << "Enter X and Y";
   cin >> rX >> rY
}


function call:
1
2
3
4
5
int main()
{
   int x, y;
   AskForXandY(x, y);
}


Why do we use the rX and rY in the prototype and the function body, but just x and y when declaring the variables and in the function call? Isn't the r part of the variable name? Doesn't removing the r and changing the case of the next letter change the variable name and make it a completely different variable?

This keeps confusing me over and over again. I look at the differences in my code and think it's wrong.
Normal function arguments are passed by "value", just a copy of the value of the variable is passed :
1
2
3
4
5
6
7
8
9
10
11
void printANumber ( int number ) // the parameter type doesn't have '&' sign, it is passed by value
{
    cout << number;
}
// ...
// ...
int main ()
{
    int x = 100; // declare a variable
    printANumber ( x ); // here we passed the value of x which is 100
}


You can visualize what happens when calling the function :
1
2
3
4
printANumber ( int number = x ) // number becomes 100
{
     cout << number; // 100 is printed
}


----------------------------------------------------

Whereas when a function takes an argument as reference, *the variable is passed*, not just the value, so we can modify it inside the function :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void changeANumber ( int& number ) // notice the '&' sign, it is passed by reference
{
    number = 243; // change the value of the variable passed
}
// ...
// ...
int main ()
{
    int x = 978;
    
    cout << x; // 978 is printed;

    changeANumber ( x ); // we passed the variable x, not 978

    cout << x; // 243 is printed, since the variable x is passed not the value of it
}


------------------------------------------------------

Why do we use the rX and rY in the prototype and the function body, but just x and y when declaring the variables and in the function call


rX and rY are, yes, they are different from x and y, rX and rY are called function parameters, they are variables that can hold the values of the variables passed as parameter when calling the function. but since askForXandY() takes a reference argument, the variables x and y are passed :

let's test your code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void AskForXandY(int &rX, int &rY)
{
   cout << "Enter X and Y";
   cin >> rX >> rY; // let's say i enter 23 and 45
}
// ...
// ...
int main()
{
   int x = 0, y = 0;

   cout << x << " " << y << endl; // 0 0 is printed

   AskForXandY(x, y);

   cout << x << " " << y; // print the value of x and y
   // The values printed are 23 45 !
}


JUst ask if you have more questions
Last edited on
So, rX and x as well as rY and y are indeed separate entities? How does the compiler know to relate them together?

Am I not understanding something very basic here? (Entirely possible.)
I just got back from class where another student was able to explain how the compiler relates the two variables to each other. It really was something very basic

[drum roll]

It's because they both have the same position in the parameter list. The function prototype, the call statement, and the function header in the definition.

Duh.
Topic archived. No new replies allowed.