software-design/pure-functions-why-the-same-call-can-give-two-different-answers.md

Pure functions: why the same call can give two different answers

Building software · 5 min read ·

Call a function twice with the same input and get two different answers? Python isn't broken. The function is doing something extra, called a side effect.

A pure function only uses its arguments and changes nothing else, so it always answers the same way. Try three small Python functions, compare each with a pure rewrite, and see why pure ones are easier to predict and test.

Two code boxes: a function that changes the cart gives two different answers, a pure rewrite gives the same answer twice

$ ls software-design/

see all →