For more information on concepts covered in this lesson, you can check out:
Pass by Reference vs Pass by Value
00:00 In this lesson, you’ll compare the notions of pass by value and pass by reference, and later, you’ll compare them to Python’s argument passing mechanism.
00:10 Much of this lesson is taken from another Real Python course I’ve done over functions in general. In this lesson, we’re going to look at a program which does the following: First, it’s going to create a variable and assign it a value.
In these examples, I’ll be calling the variable
x. That variable will then be used as the argument to a function called
f. Really inventive variable names, I know. That function will then take the associated parameter value called
fx—to mean the variable
x in the function
f—and reassign it.
It will print the value of that parameter variable both before and after it’s changed, and the main program will print the value of
x before and after the function is called.
00:59 I’ll show this to you using pass by value in C++, pass by reference in C++, and in the following lesson, you’ll see how this looks in Python. So, here is the pass by value version in C++.
You can see at the top, our function
f(), taking a single parameter called
fx is one of C++’s, simple types, an integer, this will use pass by value.
fx will get a copy of the value provided as an argument.
Here’s C++’s weird output statement. First, there is a label to describe what’s about to be displayed, followed by the value of the parameter
The parameter variable is reassigned a new value, then its new value is displayed with an appropriate label. Down here is the
It creates a variable
x and assigns it a value. That value is displayed with the label using
cout. It then calls the function
x as an argument, and finally displays the value of
x again, after the function was called. I can go into my terminal shell, compile it, and show you how it runs.
Since the argument was passed by value, the function
f() has no knowledge of the variable
x. It simply received a value of
The value of
fx was indeed changed to
10, but the original variable
x was unaffected by that.
This next version uses pass by reference. There’s only one change from the previous version to this one. That’s the use of an ampersand (
&) in front of the parameter name,
03:24 See, everything else is the same
as I switch back and forth between the two file listings. Except, of course, the name. Everything else is the same. But the
& is what tells C++ to use pass by reference for this parameter.
So this time when we use
x as the argument in the function call, the parameter
fx will be referring to the exact same piece of memory that
x is using.
That means when it changes the value of
fx here, it is also changing the value of
x. You can see that when I compile and run this program as well.
Notice this time that
x now has a value of
10 after the function is finished. That’s because it was pass by reference.
f() received a reference to
x, which it called
fx, so anything done to
fx in the function affected the value of
x outside the function. In your next lesson, you’ll see what Python’s mechanism is and compare it to both of these.
Become a Member to join the conversation.