Pass-By-Value vs Pass-By-Reference in C++
00:00 When you pass a variable as an argument to a function, there are several ways that that variable or its value can be interpreted. We’re going to take a look at a couple popular mechanisms referred to as pass-by-value and pass-by-reference, we’re going to see examples of that done in C++, and then in the lesson that follows this one, we’ll take a look at Python’s resolution to using a variable as an argument value.
00:31 When you’re using pass-by-value, a copy of the argument’s value is placed into the function. The function doesn’t know anything about where the value came from.
00:42 It doesn’t know if it was from a literal, from an expression, or from a variable. It’s just handed the value and uses it however it needs to. On the other hand, if you pass a variable by reference, then the function has a reference to that particular variable and it can actually make changes in the function that will be reflected in that variable’s value after the function finishes executing.
01:10 I’m going to use C++ as an example. The example is going to perform the same way as the Pascal version in the article on which this course is based, but I happen to have programming tools on my computer for C++, so it’s a lot more natural for me to do that.
01:29 Plus, the syntax for some things are going to look very similar to Python, because Python did borrow some of its syntax from Java, C++, languages that descended from C, and so some of these statements will look familiar. Others, not so much, but I’ll explain them as we go.
Lines 1 and 2 are similar to Python’s
import statements. They provide us with names and routines to perform certain basic tasks. In this particular case, I’m setting this up to use the standard input and output mechanisms that C++ provides. Line 3 is the beginning of a function. Instead of saying
def, which is what a Python function would begin with, we actually have to specify some type information about the function we’re writing. Since this particular function isn’t going to return a value—and we’ll talk more about return values in upcoming lessons—we say the word
Then we have the name of the function and the parameter. This is just going to have one parameter. We’re going to call it
fx—in other words,
f()’s copy of the variable
x. And in C++, we have to type all of our variables, and so we’re specifying that this as an integer. Lines 5, 7, 13, and 15 are probably the weirdest looking lines compared to everything else that you see here. These are output statements.
You begin with
cout, which is a name for where output’s going to go, and then we use the less than symbols (
<<) kind of as a direction. We’re saying we’re moving things in this direction.
03:13 So, what we’re outputting is first going to be a string literal in each one of these examples.
And then we have that
<< symbol again, where we’re going to follow that with the value of a variable. Inside the function, it’s
In the main part of our program, it’s just
x. And then the notation
endl means that that’s the end of that line and the following output should occur on the next line.
03:44 So, all of these are output statements. They have a string, the value of a variable, and then a notation to say we’re going to go to the next line.
Instead of indenting in C++, we enclose the body of our function in braces. So, here’s what our function
f() is going to do. It’s going to output the value of the parameter on line 5.
Then it’s going to change the value of its internal parameter to
10. And then line 7 is going to output that
fx’s value has changed. Since this is passed by value,
fx knows nothing of the variable that gave it its value.
04:30 It’s its own variable created anew inside this function, and we can do with it whatever we want, and changes inside this function aren’t going to be seen in our main part of our program.
In C++, you can’t just write your main program’s instructions, you actually have to put them in another function called
main(). And so this is the beginning of our
We can’t say
def because we have to say information about what the function returns in C++. In this particular case, a
main() function is supposed to return an integer for historic reasons that we don’t need to get into right now. But in my
main() function, I’m going to create a variable. I’m going to give it a value of
I’m going to output its value. Then I’m going to call the function, which is passing by value the value of
x, and we’ll see in the next program how you distinguish passing by value from passing by reference.
f() does its thing, we’re going to note that
x has not changed values.
5, we passed the argument by value to the function, the function did what it wanted to, but it had no access to the variable
And then the
return statement with a value of
0 is used to indicate that the program finished its execution successfully. So, again, I’ve got compiling tools, GNU’s C++ compiler. I’m going to specify that the result of the compiling should go to a file called
pass-by-value, and I’m going to compile
We wait a moment, and we see that that file has been compiled. I’m not going to change mode to make it executable, so I’m going to use dot-slash notation (
./) to run this program.
And we can see that before the function,
x had a value of
5 because, again, the
main() function is what’s executed when you run a C++ program.
Then we called
f() showed us the value of its parameter
fx, which is
We then changed it to
10 and displayed that output.
f() finished its execution, returned back to after line 14, and we saw that
x had not been changed. And that’s because we used pass-by-value for passing the value of
x into the function
07:18 I’m now going to show you the pass-by-reference version: what I want to do if I want the parameter to actually refer not to the value, but be another reference to the variable named as the argument.
Now, you probably didn’t see much of a difference because there’s only one character different between the pass-by-value and the pass-by-reference version of this function, and that’s the ampersand (
&) in front of the parameter.
What that means is we want the argument to be passed by reference. So when I call
f(x) down here and I use the variable
x, the name
fx in the function is actually referring to the same spot in memory that has the value of
The difference is going to be when we run this program.
x has a value of
5. We display that before we call the function and at the beginning of the function.
The function changes the value of
10, and we see that change at the end of the function. But since
fx is a another name for the variable
x, we’re going to see in line 15 that
x’s value has been changed.
08:47 So, let’s compile this. This was called pass by… well, let’s create the output.
pass-by-ref is what I want the executable file to be called,
and I’m compiling
And now the only difference in the output—which you can still see here on the terminal window, the output from
x starts at
fx gets the value of
fx changes to
10 but now, because we were passing
x by reference, we will see that the value of
x has also been changed to
And there’s that output. So again, anything that we do to
fx in this version of the function, because the parameter is being passed by reference, will have an effect outside of the calling environment.
09:52 That’s basically the difference between pass-by-value and pass-by-reference.
10:00 When you pass a variable by value, you can’t change its value in any meaningful way to be reflected outside of the calling environment. But when you pass a variable by reference, any change you make to that will have an effect after that function’s been called. In the next lesson, we’ll see how Python implements variable passing as arguments to a function.
Become a Member to join the conversation.