I use narratives to learn stuff… and to teach, too
What’s a narrative? It’s a way of connecting events or facts in a form of a story
But I promised to talk about The Function Room…
My mental image of a computer program is that of a small creature working in a room.
When you start writing a program, the Main Room is empty except for some shelves and a small red booklet called “built-in”
Monty — that’s the name of the little creature doing all the hard work — will look around the room whenever you use any name.
more in the next toot
/1
Anyway, let’s get to defining a function:
def do_something_clever(first, second):
"""do some stuff"""
output = first + second
return output
A function is a mini-program within a program.
So, in my mental image of how computer programming works, a function is another room…
When you define a function, you’re creating a new Function Room that’s adjacent to the Main Room.
There’s a door connecting the Main Room to the Function Room.
The label on the door says do_something_clever
, the name of the function.
So, when you use the name do_something_clever
, Monty will look around the Main Room and see the name as a label on a door leading to another room.
If you’ve called the function by adding parentheses, do_something_clever(5, 8)
, Monty will open the Function Room door and go through it.
/3
However, if the function call was just:
do_something_clever(5, 8)
then Monty doesn’t know what to do with the data he’s holding. So he’ll throw it away in the garbage bin so he can carry on with his work.
What if you write:
print(do_something_clever(5, 8))
Monty will go to the do_something_clever
Function Room, do what he needs to do, and return to the Main Room holding the integer 13
…
/5
…but he won’t stay there long. Monty finds the name print
in the built-in red booklet which gives him directions to the print
Function Room which is elsewhere in Python City.
He’ll leave the Main Room to go to the print
Function Room, taking the integer 13
with him.
When Monty finishes from the print
Function Room, he’ll return to the Main Room and carry on with whatever instructions come next.
Usual caveat: analogies are not perfect. So don’t take them literally. Literally!
But, our brains react better to narratives rather than random facts. So I find that these ‘stories’ help me understand and remember this stuff so much better…
Hopefully it will help others, too.
If you want to read more on this analogy, you can read this:
https://thepythoncodingbook.com/understanding-programming-the-white-room/
/6
However, he won’t go empty handed. He’ll take some data with him—the arguments you use when you call the function.
In the example in the previous tweet, Monty will take the integers
5
and8
when he goes from the Main Room to the Function Room.As Monty enters the Function Room, he’ll find two empty boxes at the entrance labelled
first
andsecond
— these are the parameters from the function definition.He’ll place the integer
5
inside the box labelledfirst
and the integer8
in the box labelledsecond
. He will then place these boxes on the Function Room shelves.Monty will then do whatever he needs to do in the Function Room.
When he’s done, he’ll return to the Main Room.
But once again, he won’t return empty-handed. He’ll take the contents of the box labelled
output
with him since this is what you wrote in thereturn
statement.Note that Monty does _not_ take the whole box labelled
output
with him but only its contents. In the example above, this is the integer13
What will Monty do with this integer as he returns to the Main Room and shuts the Function Room door behind him?
If the function was called like this:
result = do_something_clever(5, 8)
then he’ll bring another empty cardboard box, place the integer
13
inside it, and label the boxresult
. He places this box on the shelves in the Main Room./4