> What does a reference parameter/variable do in C++?

What does a reference parameter/variable do in C++?

Posted at: 2014-06-09 
I'm working on a homework assignment for my C++ class using functions for the first time. I need to use a reference variable or parameter (are they the same thing?) And I don't really understand what they do. My book says something about reference parameters passing the addresses or locations of the argument and not its value. I don't understand what that means.
A "normal" function might look like this: int add_one (int x) { return (x+1); } Now the reference parameter can let you do the same thing without the return statement. Essentially it looks like this: void add_one (int&x) { x += 1; } The '&' sign makes the x a reference parameter. When you pass a variable into a function, it is usually copied into a new instance. For example, if I said, void add_one (int x) { x += 1; } the local value of x is incremented, but the actual function has achieved nothing, since it is the copied variable that is changed. The reference parameter is different. Instead of a copy of the variable, the same variable is passed, and so whatever you do to it happens to the original variable as well (there is no original variable, actually, it's all the same one). Hope that helps.
Your homework is probably asking you to make a function that uses a parameter(what you plug into a function). For some reason, computer scientists always want to be very descriptive, but it is always counter-intuitive for people trying to learn something new. If you are already taking calc or pre-calc, think about f(x) = x^2; The parameter is x; So if you make a function to square an integer, it would probably look like this: int square (int x) { return x * x; } The parameter is inside the backets ( in here), which is int x. The function takes in an integer x as a parameter and squares it.