Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Pass by Reference vs Pass by Value

For more information on concepts covered in this lesson, you can check out:

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.

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

00:47 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++.

01:16 You can see at the top, our function f(), taking a single parameter called fx. Because fx is one of C++’s, simple types, an integer, this will use pass by value.

01:32 fx will get a copy of the value provided as an argument.

01:39 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 fx.

01:54 The parameter variable is reassigned a new value, then its new value is displayed with an appropriate label. Down here is the main() function.

02:06 It creates a variable x and assigns it a value. That value is displayed with the label using cout. It then calls the function f(), passing 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.

02:49 Since the argument was passed by value, the function f() has no knowledge of the variable x. It simply received a value of 5.

03:01 The value of fx was indeed changed to 10, but the original variable x was unaffected by that.

03:11 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, fx.

03:24 See, everything else is the same

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

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

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

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