Skip to main content

Pure and Impure Functions

Pure Functions

A Pure Function is predictable: given the same input, it always returns the same output and has no side effects (it doesn't change anything in the outside world).

# Pure: returns a result without changing anything outside
def pure_chai(cups):
return cups

Impure Functions

An Impure Function, however, modifies external state or relies on global variables. These are generally harder to test and maintain because their results can change depending on the state of the program.

total_chai = 10

# Impure: modifies a global variable
def impure_chai(cups: int) -> None:
global total_chai
total_chai += cups # Side effect: changing global state
info

It's recommended to avoid usinng impure functions