This lesson will introduce you to one of the cornerstones of programming, namely functions. Functions 1
go by many names and other programming languages functions have been called subroutines, methods procedures, 2
but don't let the jargon get to you. 3
The central idea is always the same. In Shakespeare's Romeo and Juliet, 4
Juliet famously said "A rose by any other name would smell just as sweet". 5
At the time she was talking about Romeo. 6
But had she been a programmer, I'm sure she would have said the same thing about functions. And on that 7
note, 8
I hope you're ready to start your love affair with functions. 9
Our goal in this lesson is to explain exactly what's going on with a line of Python code like this - show, 10
followed by the parentheses. 11
We're gonna be focusing on the part after the dot. 12
That way, we can break down the programming concepts of functions into bite sized lessons and build up 13
to the big picture. 14
As such, 15
this lesson really sets up the next couple of lessons on arguments, return values and on objects, so 16
you can think of these videos like a combo - like bread and butter, or burgers and fries or a jab and a cross, 17
or Coca-Cola and Mentos. 18
You get the point. 19
So what's so great about functions? 20
Well, imagine I'm your little housekeeping robot because, I don't know, you didn't want to buy a Roomba or 21
something. And if I was a human you could tell me "Hey, go and buy me some milk". 22
That instruction would make sense to a human. 23
A human would understand what to do in order to complete this task. 24
However, when we're giving instructions to a robot, it doesn't quite work like that. A robot doesn't know 25
what getting milk involves, especially if you've bought the base model with no machine learning capabilities. 26
So when you're dealing with a robot, you have to specify every single step of this task, like taking the 27
money, opening the door, closing the door, walking to the store etc etc etc. You have to specify every 28
single instruction. 29
Now, come the next day and you need to get more milk. 30
Well, you have to specify all these instructions again and that would be incredibly annoying and you'd 31
probably bin your housekeeping robot in short order. 32
Here's where functions come to the rescue. With a function you can bundle all these individual instructions 33
into a single instruction, and we can also give this function a descriptive name like get_milk. 34
Now, every time we want to use get_milk, 35
the housekeeping robot knows exactly what to do. 36
It will execute all the instructions contained inside this function. 37
So there's something to note here - what we just described is actually a two step process. 38
First - you have to tell the robot what get_milk means, and second - you have to tell the robot to execute 39
this get_milk instruction. 40
In other words, step 1 is defining or declaring the function and step 2 is using or calling the function. 41
The syntax looks like this - when defining the function you'll always see this keyword - def. 42
Def followed by the name of the function, followed by parentheses, followed by a colon. After the colon, 43
on the lines below, 44
you have the instructions inside the function. 45
This group of statements is going to be indented and the indentation tells us what statements are inside 46
of the function. 47
Now there's actual words for the anatomy of a function. 48
While you and I have got a head and a body a function has got a header and a body - and the header is 49
this line right. It's the line with the def keyword at the top, and the body are all the statements, all 50
the instructions that are inside the function. 51
Basically, all the lines that are indented. That's the body. 52
Now let's take another look at this line of code where it says plt.show(). The parentheses 53
at the end are telling us that show is a very different beast from math.pi, or 54
hitchikersGuide.theAnswer. 55
And that's because show is a function, because it had these parentheses at the end. 56
Now there's no parentheses after a math.pi, and there's no parentheses after 57
hitchikersGuide.theAnswer. 58
These two are not functions. 59
So now that we know a little bit about what a function is supposed to look like, let's create our own 60
first function. Scrolling to the bottom of our Python intro notebook, 61
we can write def get_milk, and then parentheses followed by a colon and when we hit Enter 62
we'll get taken to the next line which is indented and here we can print out some instructions like 63
"Open door", and then we can hit Enter again, 64
get taken to the next line which again is indented and follow up with some more instructions that 65
are going to go inside the function, like print("Walk to the store"), and then print("Buy milk on the ground 66
floor") 67
And we'll finish it up with printing 68
"Return 69
with milk galore". 70
So in our function body, we have for individual print statements. 71
Notice that when you hit Enter on your keyboard Jupyter notebook already indents these lines for you 72
and we have an indentation of one, two, three, four spaces. In Python, 73
this indentation or whitespace actually matters quite a bit. Indentation in Python, 74
is like a full stop at the end of an English sentence. Just like the full stop at the end of the sentence, 75
the whitespace helps Python determine where one instruction ends and another one begins. 76
Now, just like Jupyter notebook has shown us, you also want to have four spaces between the beginning 77
of a line and the first print statement. 78
Usually, the text editor that you're working in, or Jupyter notebook in this case, will help you indent 79
this correctly for you. 80
Let's hit Shift+Enter on the cell and see what happens. 81
Nothing, right? 82
Nothing at all. 83
And that's because we've only defined the function - we actually haven't executed it yet. 84
Remember how we were talking about the two step process? 85
Let's call the get_milk 86
function now. 87
With me write get_milk() and hit Shift+Enter. 88
Now I'm noticing that I made a typo, so let's fix this. 89
Gonna hit Shift+Enter again and execute this function again. 90
Okay, that was really bothering me. 91
Below the cell where we're calling the function, where we wrote get_milk() 92
we see the four statements from the function being printed out. 93
The other thing I quite like about Jupyter notebook is that the syntax highlighting here actually helps 94
us differentiate visually and see at first glance on which part we're defining the function and on which 95
part we're calling the function. The part where we're defining the function the function name is highlighted 96
in blue, and the part where we're calling the function, 97
it's regular black Python code. 98
Now let's take it up a step in complexity in the next lesson.