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

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands 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 your subtitle preferences 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 refer to our video player troubleshooting guide for assistance.

Return and Replace

00:00 One of the top best practices for mimicking pass by reference is with something called return and replace. You’ve actually seen this before in the lessons about the customized greetings, but let’s look at this in a new example.

00:14 A common pass by reference example is a function to square a variable, with the variable to retain that new value after the function is called. Here’s a simple C++ function to demonstrate that. The function takes an argument by reference and actually replaces that value with its square.

00:33 Here, num is passed by reference. It is then multiplied by itself and saved back to the same variable. Nothing is returned. The side effect of changing the argument is the intent of the function.

00:49 In the main() function to try it out, we create a variable with the value 4, use that as the argument to pass by reference to the squareRef() function, and then it displays the new value, which should be 16.

01:04 Let’s try this out.

01:12 Again, I can’t run C++ code interactively, so I have to compile the C++ file and then run it,

01:22 and we do see we get the output of 16. The value of val was indeed changed using pass by reference. So, how would you replicate this in Python?

01:37 The technique is called return and replace. Since the function can’t reassign the new value to the argument variable passed, you as the programmer must take the steps to make that happen, which means the function has to return the new value, and when calling the function, the return value needs to be saved back to that same variable.

02:01 So, here’s that same Python square() function you saw in one of the earlier lessons. It returns the square of the argument provided. I’ve commented out the script you saw before, because I want to use this function interactively this time.

02:20 So, let me import that file.

02:25 So, return and replace: We have a variable we wish to change. We want to replace val with its square. So we take the return value from squaring val and replace it back to the variable val.

02:44 val has now been changed to 16. If a function is to modify multiple variables, just return the new values and unpack the tuple back into the desired variables, just like you saw with that greeting function from earlier.

03:03 I won’t import anything, I’ll just go ahead and write this out.

03:12 Again, remember this returns a greeting personalized to the name provided as the first parameter and then increments—well, adds 1 to the counter, which should be saved back to the same variable passed as an argument, to create the effect of incrementing it by 1.

03:31 So, let’s create our count variable. Right now, we’ve called that function zero times. When we call it, we get two values back in a tuple, and we can unpack it simply by assigning each one to a separate variable.

03:45 The variable count, we want to mimic pass by reference with return and replace, so we pass it as the parameter to be modified, and when it’s returned, we replace that variable with its new value.

04:04 So, we can see count has been incremented to have a value of 1. greeting is the greeting, but it was count that we were returning and replacing.

04:15 So, that’s return and replace. In the next lesson, we’ll look at the best practice of using object attributes.

GrapesRPeeps2 on Feb. 28, 2024

I find it a bit hard to remember what the code looks like when the view is switched to the terminal, where the code is run. I would find it much easier if the code and the terminal where the code is run were side-by-side.

Tappan Moore RP Team on March 2, 2024

Hi, I’m the video editor for realpython. Thanks for your comment, I’ve made a note of it for courses going forward.

Become a Member to join the conversation.