For more information on concepts covered in this lesson, you can check out:
Pass by Reference vs Pass by Value
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 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.
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.