Function Basics (Passing by Reference)

At this point we need to learn a couple of important concepts about how Python handles variables/objects in the memory. We need to understand this so that we can better understand what happens when we pass variables to functions. Understanding this will prevent “weird issues” that might come up in your code when passing values to your functions.

Variables = whenever you are using default data-types (int, float, string, bool)

Objects = whenever you are using a data structure (list, dictionary, sets, tuples).

Mutable and immutable:



All variables (default data-types) + tuples are immutable.

All other objects are mutable.

Now, we say that something is immutable when its value cannot be changed in the same memory location.

Mutable means that the value in the memory can be changed.



The previous image shows how memory is managed in Python. Whenever you change the value of an integer (= immutable = value cannot change in the same memory location), then Python creates a new memory location with the new value. This is shown in the first image.

When you say x = x + 10, then Python creates a new memory location for “x + 10” and x now points to this new memory location.



But lists are said to be mutable. What does this mean? It means whenever you change the existing value of a list (by append, del etc) then its memory location will get updated.



Understanding how they work is critical to know what will happen when you’re using them. Now see the example below and try to predict the output before running it. Do NOT run the output. First pause and try to figure this out on your own based on the concepts explained above:




What happened over here? list1 and list2 were both accessing the same location. list1 was appended. Since list2 accesses the same memory location, and hence list2 also has the same value.

But that’s not the case with integers. Every time you perform an operation on an integer, a new memory location gets created. Hence, x and y have different memory locations and different values.

Now that you know how variables and objects are stored in memory, we can move into how “passing values to functions” works in the memory.

Passing values to functions:

If you understood the previous concept then this one should be easy for you to figure out.

Whenever you pass some value to a function you are passing the original value, i.e., your function initially accesses the original memory location. But the function may or may not modify the original memory location depending upon what it is doing.

Consider when you pass an integer or list to a function and then inside the function you change assignments. Check out the following image:



So far everything works as expected. But now consider that you pass a list and inside the function you modify the list. This is where object mutability plays a role! See the image below:

When you’re passing and changing a list, dictionary or set (mutable objects) inside a function then it will also change the value outside it.



Before you run the next example, pause and think what should be the output. Go through the previous two images again and try to guess what the output should be.