I just started C++ and I don't quite understand the difference between pass by value vs reference. It seems like the mechanisms are the same. For example, by passing a value in a function, I am told that I am passing by value. The function creates a "copy" of my argument(s) and it just exists in the scope of the function. When I pass by reference, it is not the same thing?
void square(int &x) {
x *= x;
}
int main() {
int a = 3;
square(a);
std::cout << a << std::endl; // prints 9
return 0;
}
In this example, my function receives an address of an integer as an argument. This address is "copied" somewhere in memory, let's say &x = 0x1. This function is only aware of &x and not x, so when I call x, I'm calling *(&x) which is *(copy of &x) which is the original x. What I'm asking is basically is passing by reference simply pass by value of the address?