As it’s time for my morning coffee (the coffee not the biscuits), it’s as good a time as any to share my one of my favourite analogies

(narrated from a -viewpoint but general enough for in general)

The Coffee Machine - Function analogy

Let’s make some coffee…

[read on]


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)


Show thread

— 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!


Show thread

— 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…

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.


Show thread

— 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…


Show thread

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…


Show thread

You called the function like this:

>>> make_coffee(
... electricity,
... water,
... my_favourite_blend,
... )

and therefore, the coffee returned is not collected and stored in anything…


Show thread

Instead, you should put a coffee cup in the machine when you turn it on:

>>> cup = make_coffee(
... electricity,
... water,
... my_favourite_blend,
... )

Now, you no longer make a mess on the kitchen bench you’re collecting what’s returned into something

There’s a fuller version of this analogy here, too:


Show thread

Addendum (some sad news): Since taking those pictures for the article, that sole remaining espresso cup has suffered this fate

I was told I can’t say Rest In Pieces

Show thread

@s_gruppetta ok. The pun was funny 😆
Sorry that that happened though 😢 ☕️

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.