Python Function Value Passing vs. Reference Passing: Explained with Examples

In Python, when you work with functions, it’s crucial to understand how values are passed to functions and whether they are passed by value or by reference. This distinction is essential for writing efficient and bug-free code. In this article, we will explore the concepts of value passing and reference passing in Python, highlighting the difference between formal and actual parameters, and providing illustrative examples.

1. Formal Parameters vs. Actual Parameters.

  1. Before diving into value and reference passing, let’s clarify the distinction between formal parameters and actual parameters:
  2. Formal Parameters: These are placeholders in a function definition that represent the values the function expects to receive. Formal parameters are used within the function’s scope to perform computations.
  3. Actual Parameters: Also known as arguments, these are the values passed to a function when it is called. Actual parameters provide concrete values that are substituted for formal parameters during the function’s execution.

2. Value Passing.

  1. In Python, primitive data types (integers, floats, strings, etc.) are passed by value.
  2. This means that when you pass a primitive data type to a function, a copy of the value is created, and any modifications made within the function do not affect the original value outside the function.
  3. Let’s illustrate this with examples.

2.1 Example 1 – Integer passed by value.

  1. Source code
    def modify_value(x):
        x += 10
        print("Inside function:", x)
    value = 5
    print("Outside function:", value)
  2. Output:

    Inside function: 15
    Outside function: 5
  3. In this example, `value` remains unchanged outside the `modify_value` function because integers are passed by value.

2.2 Example 2 – String passed by value.

  1. Source code.
    def modify_string(s):
        s += " World"
        print("Inside function:", s)
    my_string = "Hello"
    print("Outside function:", my_string)
  2. Output.
    Inside function: Hello World
    Outside function: Hello

2.3 Example 3 – Tuple passed by value.

  1. Source code.
    def modify_tuple(t):
        t += (4, 5)
        print("Inside function:", t)
    my_tuple = (1, 2, 3)
    print("Outside function:", my_tuple)
  2. Output.
    Inside function: (1, 2, 3, 4, 5)
    Outside function: (1, 2, 3)

3. Reference Passing.

  1. In Python, complex data types (lists, dictionaries, objects, etc.) are passed by reference.
  2. This means that when you pass a complex data type to a function, you are passing a reference to the same memory location, and any modifications made to the object inside the function will affect the original object outside the function.
  3. Here are some examples.

3.1 Example 1 – List passed by reference.

  1. Lists are mutable objects, and changes made inside the function will affect the original list.
  2. Source code.
    def modify_list(my_list):
        print("Inside function:", my_list)
    my_list = [1, 2, 3]
    print("Outside function:", my_list)
  3. Output:

    Inside function: [1, 2, 3, 4]
    Outside function: [1, 2, 3, 4]
  4. In this example, `my_list` is modified both inside and outside the `modify_list` function because lists are passed by reference.

3.2 Example 2 – Dictionary passed by reference.

  1. Dictionaries are also mutable, and changes made to a dictionary inside a function persist outside the function.
  2. Source code.
    def modify_dict(my_dict):
        my_dict["key"] = "new_value"
        print("Inside function:", my_dict)
    my_dict = {"key": "value"}
    print("Outside function:", my_dict)
  3. Output.
    Inside function: {'key': 'new_value'}
    Outside function: {'key': 'new_value'}

3.3 Example 3 – Class Objects passed by reference.

  1. Objects of custom classes are also passed by reference, allowing you to modify their attributes.
  2. Source code.
    class MyClass:
        def __init__(self, value):
            self.value = value
    def modify_object(obj):
        obj.value = "new_value"
        print("Inside function:", obj.value)
    my_object = MyClass("original_value")
    print("Outside function:", my_object.value)
    print("Outside function:", my_object.value)
  3. Output.
    Outside function: original_value
    Inside function: new_value
    Outside function: new_value

4. Mutable vs. Immutable Objects.

  1. The key to understanding value passing vs. reference passing in Python lies in the distinction between mutable and immutable objects:
  2. Mutable Objects: Objects that can be modified after creation (e.g., lists, dictionaries, sets) are mutable. Changes made to these objects inside a function persist outside the function.
  3. Immutable Objects: Objects that cannot be modified after creation (e.g., integers, floats, strings, tuples) are immutable. Changes made to these objects inside a function do not affect the original object outside the function.

5. Conclusion.

  1. In Python, understanding how values are passed to functions, whether by value or by reference, is crucial for writing robust code.
  2. Primitive data types are passed by value, while complex data types are passed by reference.
  3. This distinction is essential for preventing unintended side effects when modifying objects within functions.
  4. By grasping the concepts of formal and actual parameters, as well as the mutability of data types, you can write more predictable and maintainable Python code.
  5. Whether you want to maintain the integrity of the original data or work with modified copies, understanding value and reference passing is fundamental to effective Python programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.