Almost everything in this path rests on one idea, so we start there: the pure function.
A function is pure when both of these hold:
- Its output depends only on its inputs. Same arguments in → same answer out, every single time.
- It has no side effects. It doesn’t change anything outside itself — no writing to a global, a file, a database, or the screen; and it doesn’t modify the arguments it was handed. It just takes values and returns a value.
A function that breaks either rule is called impure.
Here are four small functions (Python, but the shape is universal — def name(args): body, and
return hands a value back to the caller):
TAX = 0.08
cart = []
# A
def add_tax(price):
return price + price * TAX
# B
def add_to_cart(item):
cart.append(item) # cart is defined outside the function
return cart
# C
import time
def now_plus(seconds):
return time.time() + seconds # time.time() = seconds since 1970, right now
# D
def greet(name):
print("Hi, " + name) # writes to the screen; returns nothing
Exactly one is pure. Which one — and, for each of the other three, which rule does it break?