One of my favourite programming & Python analogies…
The Coffee Machine—Function analogy
Let's make some coffee…
Read on
👇🪡🧵
1/
A function is like a coffee machine
1️⃣
It needs inputs to work (arguments):
— water
— electricity
— ground coffee beans (or pods if so inclined)
You can choose to put different blends of coffee into your machine (choice of what argument to pass to function)
2/
2️⃣
You press the "On" button—this is equivalent to calling the function.
You can almost see the similarity between the typical "On" button and the parentheses ( ) used to call a function in Python!
3/
3️⃣
When you call the function/press the "On" button on the coffee machine, the machine will "do stuff" inside.
If you just care about drinking your coffee, you don't care what's happening inside the machine…
4/
If you're an engineer building your own coffee machine, or you want to open up your coffee machine to fix it or—ahem—make improvements, then you _do_ care about what's happening inside the coffee machine.
5/
4️⃣
When the coffee machine does its thing, hot, liquid coffee will emerge from the machine.
You could say the coffee machine _returns_ liquid coffee.
but, there's one problem…
6/
5️⃣
The coffee flows directly into the drainage tray at the bottom of the machine and will likely overflow, making a mess on your kitchen bench…
7/
PS: if you want to look at this analogy from an OOP perspective, then `CoffeeMachine` might be the class and `.make_coffee()` would be a method of the class.
10/
If you enjoyed this thread you can:
— treat yourself by making a coffee (if you drink coffee and it's not too late in the day)
— follow me @s_gruppetta_ct for more Python-caffeine
— retweet the first tweet below so others can enjoy your favourite blend of coffee