Reputation: 21
I'm a beginner learning C++ and reached a chapter regarding passing arguments by references. There was a swap function defined as such:
void swap(double& d1, double& d2)
{
double temp = d1;
d1 = d2;
d2 = temp;
}
The test was this, and it worked, with values swapped:
int main()
{
double x = 1;
double y = 2;
cout << "X==" << x << "y==" << y << '\n';
swap(x,y);
cout << "x==" << x << "y==" << y << '\n';
}
However I can't for the life of me figure out why removing the references makes this code do nothing:
void swap(double d1, double d2)
{
double temp = d1;
d1 = d2;
d2 = temp;
}
I've done this since I wanted to better understand references since properly understanding them eludes me. I would appreciate an answer or a pointer in the right direction.
Upvotes: 1
Views: 95
Reputation: 8589
There are common notions in procedural programming languages like C++ of "pass by value" and "pass by reference".
When a variable is passed by value into a procedure (or function) a copy is made of the variable and any modifications made in the procedure (or function) don't reflect in the variable passed by value.
Conversely, when a variable is passed by reference modifications are reflected in the variable that was passed by reference.
The term reference in C++ refers to that idea so in the reference case the actions of swap()
do swap the original variables passed as arguments, in the second case they don't.
If you see a function void foo(int x)
called as foo(b);
you should read that as "make a copy of b
and perform foo
on that copy".
If it were void foo(int &x)
it would be "perform foo
directly on b
.
Upvotes: 3
Reputation: 688
Because by default, when you pass a variable as a function argument, it's passed by value. That means that (like in a code below) when passed to the function, it actually creates a copy of it and you no longer work with the original variable and you operate with its copy - it has the same value but different location in memory.
int myVar = 10;
void f(int var) {
var = 20;
}
// here the value of myVar is 10
f(myVar);
// here the value of myVar is still 10
Passing variable by its reference is there exactly because of the example you showed. You want to create a function that can actually modify the value of a passed variable outside of the scope of the function.
int myVar = 10;
void f(int& var) {
var = 20;
}
// here the value of myVar is 10
f(myVar);
// here the value of myVar is still 20
You can think of it as a definition
This is very often used when working with strings. For example: let's assume you get packet data in a variable std::string packet
and you want to create a function that analysis it. You can do something like this
std::string packet;
someResultType func(std::string packet) {
....
}
auto result = function(packet);
But in this example, it is totally useless to create a new copy of variable packet because when working with it, you probably won't modify it, so you say to the compiler - I promise I won't modify the value thus you can pass the value by reference:
std::string packet;
someResultType func(const std::string& packet) {
// you're working with the global packet but you cant modify it
}
auto result = function(packet);
Upvotes: 0
Reputation: 66441
Passing a reference is like passing "the thing itself", not just its value.
It works kind of like if you handed a friend your precious ring, let them muck around with it in whatever way, and then got it back.
Passing by value is like handing a friend a different ring that looks exactly like your precious ring and they get to keep it, and you keep yours, untouched by their filthy, thieving fingerses.
A simpler example than swap
:
void fv(int x)
{
x = 0;
}
If you pass a variable to this, that variable's value is copied into the completely indepent variable x
.
x
will have the same value as your variable, but it will not be the same thing.
When you assign a value to x
, you're assigning to this variable, not to the one whose value was copied into it.
void fr(int& x)
{
x = 0;
}
If you pass a variable to this - fr(v)
- x
is your variable; the names "x" and "v" refer to the same thing.
Assigning a value to x
inside the function is exactly the same as assigning a value to v
in the calling code.
Upvotes: 1
Reputation: 122830
Lets use an analogy... you have a piece of paper with two numbers written on it. Now you want a friend to swap the two values. This:
void swap(double d1, double d2) {
double temp = d1;
d1 = d2;
d2 = temp;
}
is like you take a second piece of paper, write the numbers on it, give it to your friend (pass-by-value), he swaps the value but returns nothing to you (void
return type). Will that change change the numbers on your piece of paper? No.
Lets change the strategy: Instead of giving your friend a copy of your numbers, you now tell him where he can find your piece of paper so he can do the job directly on that (pass-by-reference):
void swap(double& d1, double& d2) {
double temp = d1;
d1 = d2;
d2 = temp;
}
He still does not have to return anything to you, because once he is done you just have to look at your piece of paper to see the values swapped.
PS: analogies always have their limit, so take it with a grain of salt.
Upvotes: 2
Reputation: 365
In the first example, you are passing the doubles as reference. Altering the values inside the function, changes the double x & y in your main.
In the second example, you are passing the doubles as value. This means, their value is copied ie. two new doubles are created and passed to the function. By changing those values, you modify the copied doubles - and not x & y from your main.
Upvotes: 0