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: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.
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.
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
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 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.
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.
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.