🧭Pure Function
Also known as Pure Functions
A function that gives the same answer for the same input. It changes nothing else.
Think of it like
Think of it like a calculator. Press 2 plus 3, and you get 5 every single time.
Example
Jack's add(2, 3) gives 5 today, tomorrow and on every computer. His test only checks that one number.
Why it matters
Pure functions can be tested alone, cached and run in any order without surprises.
Definition
Example code
python
def add(a, b):
return a + b
print(add(2, 3)) # 5Common mistake
Key takeaways
- 1Same in, same out. Every time, everywhere.
- 2No side effects. Nothing outside is changed.
- 3Easy to test. Check the answer, nothing else.