Content · Programming · Python

Python Pure Functions vs Functions with Side Effects

A pure function returns a result determined only by its inputs and avoids observable side effects. A function that prints, changes a global, writes a file or modifies caller-owned data has side effects.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

This guide explains pure functions vs functions with side effects through a focused Python example. You will learn the behavior to expect, why it matters in a real program, and how to recognize the common mistakes before applying it in your own project.

What You Will Learn


A pure function returns a result determined only by its inputs and avoids observable side effects. A function that prints, changes a global, writes a file or modifies caller-owned data has side effects.

When and Why to Use It


The following example focuses on a practical use of Pure Functions vs Functions with Side Effects.

Step-by-Step Example


def add_tax(subtotal, tax_rate):
    return subtotal * (1 + tax_rate)

def show_total(subtotal, tax_rate):
    print(add_tax(subtotal, tax_rate))

print(add_tax(100, 0.2))
show_total(100, 0.2)

Understanding the Example


add_tax computes the same result for the same inputs. show_total adds a display side effect even though it reuses the pure calculation.

Expected Output


120.0
120.0

Important Behavior and Edge Cases


Purity is a design property, not a built-in Python annotation. Avoid mutating a list supplied as an argument when you intend a pure transformation.

Try It Yourself


Rewrite a function that appends to an argument list so it returns a new list instead.

Common Beginner Mistakes


  • Purity is a design property, not a built-in Python annotation. Avoid mutating a list supplied as an argument when you intend a pure transformation.
  • Confusing a returned value with printed output, or failing to verify a function’s behavior with different input values.
  • Skipping the practice step and assuming the example covers every input. Test at least one normal case and one edge case relevant to this topic.

Key Takeaways


  • A pure function returns a result determined only by its inputs and avoids observable side effects.
  • add_tax computes the same result for the same inputs.
  • Apply the technique deliberately, checking its documented input requirements and failure behavior before integrating it into a larger project.
Share this resourceSend this page to someone who may find it useful.