Now that we've covered the function basics, we can take it up a notch in complexity. In this lesson, 1
we're gonna talk about functions with inputs, namely parameters and arguments. 2
So I realized that if you're new to programming you're going to be hearing a lot of new words in these 3
lessons. 4
And one thing I'll say is that when you're learning anything new, 5
the hard thing isn't necessarily the concepts, 6
it's often the language. And it doesn't really matter if it's programming or medicine or cooking; 7
every field has its own jargon and its own words. 8
A big part of learning Python programming is learning some of this vocabulary. 9
I'm going to be doing my best to introduce this vocabulary slowly over the course of the lessons. Now 10
I realize that learning vocabulary words doesn't sound very fun. 11
But the upside is that you're not gonna be learning words that people invented just to sound smart. 12
Come to think of it, the Olympic champions of inventing meaningless words and catchphrases are probably 13
the business consultants of this world. 14
I've heard people say like the most ridiculous stuff with a completely straight face. 15
I think my all time favorites are words like 'helicoptering' - which apparently means looking at things 16
from above, and another one 'peeling the onion' - which apparently means doing more analysis. 17
So let's get back to Python programming, otherwise I won't be able to keep my lunch down. 18
Looking at the code we've written previously we can see that some functions take inputs. 19
So for example, this use function here had an input called 'fivethirtyeight' and our read_csv function had an input that 20
was a file name. All of these inputs to functions go in between the parentheses after the function name. 21
In contrast, our get_milk function had no inputs and this is the same with the show function, which also had 22
no inputs. 23
In contrast, the read_csv function had a piece of text between the parentheses and this piece of text 24
was the input to the function. These inputs are called arguments or parameters. 25
Let's create our own function that takes an input. 26
That way we can put all of this into practice and see how the code actually works. 27
I'm going to quickly copy paste this code from the get_milk function into my cell below and I'm gonna 28
modify this code. 29
First, I'm going to give this function a different name. 30
I'm gonna call this function fill_the_fridge. Also, inside these parentheses, 31
I'm going to specify an amount. This amount is gonna be our input to our function. Finally, 32
I'm going to modify our third print statement down here. 33
So where it says buy, I'm going to use the single quotes and the plus sign and then I'm going to put amount 34
here. 35
Like so. 36
So it reads 'buy' + amount + ' cartons on the ground floor'. This way 37
I'm making use of the input within the body of our function. The plus sign in this print statement concatenates 38
our strings and by concatenate I mean merge or combine. 39
Let's call this function to see it in action. 40
So I'm going to hit Shift+Enter, and then I'm going to write fill_the_fridge, which is the name of the function 41
followed by the parentheses. 42
And then within the parentheses I'm going to put single quotes and write "five", and hit Shift+Enter. 43
Now the piece of text that we entered between the parentheses shows up in our print statement. 44
Let's think about what just happened here. 45
First, you'll notice that the plus sign in Python actually means different things in different contexts. 46
When the plus sign is used with numbers it adds them together. 47
But when the plus sign is used with text it combines the strings. 48
Let's call this function again but this time let's substitute a different amount. 49
I'm going to write 50
fill_the_fridge('one thousand') and hit Shift+Enter. Let's look at this code and think very carefully 51
about what's going on. 52
Our fill_the_fridge function takes one input, that input has a name and is called amount. 53
But the really cool part is how amount is just a place holder. 54
The value of amount is determined by whatever we specify when we call our fill_the_fridge function. 55
The first time we specified the value "five" and amount held on to that piece of text. 56
So the word "five" was printed in the print statement when we executed the function. 57
But the second time we substituted the word "one thousand", so that the amount took on this value instead. 58
And we saw "one thousand" printed out on the third line. 59
In other words, amount behaves just like a variable - it gets its value when we call the function. 60
And every time we refer to amount in the body of the function, we are able to use the value that's stored 61
inside. Now there's two other things that you might notice about the code we just wrote. 62
First, let's try to call this function with empty parentheses. 63
So I'm going to write fill_the_fridge separated by underscores, and then have empty parentheses and hit 64
Shift+Enter. In this case we get an error and the details of this error message read "Missing one required 65
argument: amount". 66
In other words, because we've defined our function with an input we must provide it. We specified one 67
parameter in the function header so we must give a value. 68
We can't just leave it out. 69
The other thing you'll notice at this point is that print is nothing other than a function as well. 70
Print takes a piece of text as an input and prints it out below the cell. So if print is a function, that 71
means we're calling a function inside our function - we're nesting our function calls. Functions inside 72
functions is like, I don't know, dreams within dreams in the movie Inception. 73
In other words, one piece of code can trigger other bits of code. 74
And this idea is actually really, really powerful when you think about it. 75
So here's a quick recap. 76
When working with functions with inputs, we specify what the input is upfront when defining the function. 77
The input is specified between the parentheses following the function name. In this example, the input 78
is called money and money is just a place holder. In programming jargon when referring to an input as 79
a place holder or as a variable, 80
we tend to call it a parameter as in: get_milk has one parameter called money. 81
Now, remember how I said we're going to have some vocabulary training? 82
Well, when we call the function we must provide a value to this parameter. 83
In this case we must provide a number - that value is gonna be 20.5 when I call this function. 84
The value that's passed to a function when it's being called 85
is called an argument. 86
Now oftentimes you'll see the words parameter and argument used interchangeably. 87
People aren't always super pedantic in the real world, but if you want to express yourself precisely 88
or get technical it's good to know these definitions. A parameter is when we're referring to the inputs 89
to a function and we're talking about it as the place holder or as the variable and we use the word 90
argument when we're talking about the actual value that we're giving the function when we're calling 91
it. But, 92
that's probably enough vocabulary training for now. And we've also covered quite a few different kinds 93
of functions so far. 94
For example, we understand the syntax behind show and we also understand the syntax behind use or read_csv. 95
Show had no arguments, but read_csv has a file name as an argument. 96
Now we're gonna look at functions with multiple arguments. 97
We've actually encountered multiple arguments when we called the scatter function for our plot and we 98
also had multiple arguments when we called the fit function for our regression - fit had two arguments 99
capital X and lowercase y. X was our movie budgets and lowercase y was our movie revenue. 100
We can see that these two arguments are separated by a comma, but let's create our own function and have 101
it take more than one argument so that we can see this in practice. Back in the Python intro notebook, 102
I'm going to copy our previous function that we defined here - the fill_the_fridge function and I'm going 103
to paste that into the cell where we had the error. 104
I'm now going to modify this function; first by changing the name. 105
So I'm going to call this function milk_mission and then I'm also going to change the function 106
header. 107
I'm going to change the number of parameters that are between these two parentheses. 108
So after amount I'm going to add another parameter. 109
I'm going to add a destination. 110
We're going to use this parameter in our second print statement. 111
Which, instead of reading "walk to the store" we're going to have it read 112
'walk to the' + destination 113
So we're gonna concatenate the string walk to the with whatever is supplied as a destination. 114
I can't wait to try out our new function. 115
So I'm going to hit Shift+Enter and then in the cell below I'm going to call this function. 116
I'm going to say milk_mission 117
and for amount I'm going to write 'twenty' and for destination I'm going to write in single quotes 118
'department store'. 119
Here we can see that our first argument 'twenty' is gonna be for the amount and our second argument 'department 120
store' is gonna be for destination. When I hit Shift+Enter, 121
I can see these values substituted into our print statement accordingly. 122
Now here's the question - what do you think will happen if instead of writing milk_mission('twenty', 'department 123
store') we write milk_mission('department store', 'twenty')? 124
You guessed it. I walk to the 'twenty' and I buy 'department store cartons of milk'. What this tells us is that 125
our arguments to our function are passed by their position. 126
In other words - the order in which we give the inputs to our function matters and it matters quite a 127
bit. 128
The arguments are passed from left to right. 129
So, as before, we must supply exactly two arguments when recalling milk mission but we also must be careful 130
to supply these arguments in the right order. 131
Now, looking at the syntax for calling a function, it still strikes me kind of strange every time I think 132
about it. 133
Imagine how that would play out in real life, 134
imagine your girlfriend or boyfriend telling you to go to the supermarket and saying go buy eggs, potatoes, 135
milk cartons, doughnuts and apples by 2, 10, 1, 12 and 5. 136
This is confusing, right? 137
And yet many programming languages work exactly like this. But confusion is bad and nobody wants to come 138
back from the supermarket and have an argument with their partner because they've messed up the order 139
of their arguments. 140
Lucky for us, Python can actually help us have a successful relationship. 141
It provides an alternative for passing arguments by position. 142
We can also match our arguments by providing a name. Now, believe it or not, we actually did this previously 143
in our notebook. 144
Check out how we called scatter for instance - we had X, y, color = 'blue', alpha = 145
0.3) 146
The point that I want to draw your attention to is the part where it says color = 'blue' and alpha = 147
0.3 148
Here we are matching our arguments by name. 149
Let's go back to our milk_mission function. Remember how when we got the order wrong, we printed nonsense? 150
Well, we can add the name of our parameters to the function call 151
so that Python will match these values by keyword. 152
So if we say destination = 'department store' and amount = 'twenty' and press Shift+Enter, 153
we can see that the output below the cell updates to the way that we wanted to, namely "Walk to the department 154
store", "Buy twenty cartons on the ground floor". 155
So here we are setting the destination equal to 'department store' and we're setting the amount equal to twenty. 156
Looking at the code where we called scatter again, 157
we can actually see that in this case we're kind of mixing and matching supplying arguments by position 158
and by name. The equivalent when we're calling milk_mission would look something like this 159
we'd have milk_mission('twenty', destination='store') and hit Shift+Enter. 160
In this case the argument 'twenty' is set by position and the destination is set by name. 161
Now, personally I actually quite like named arguments. 162
They make the Python code much more readable. 163
It's like having labels for the data. 164
Suppose I went back where we called scatter and I deleted the names for these arguments. 165
In this case we just have X, y, 'blue', 0.3. 166
Anybody looking at this code is gonna wonder what on earth this 0.3 and that somebody might 167
as well be yourself in like two weeks time when you forgot what you did. 168
I guess the message I'm trying to convey here is that having a bunch of naked values separated by commas 169
doesn't make the code very readable to us humans. Providing these keywords and calling a function just 170
makes everything so much more readable. 171
So here's a question for you - what do you think will happen if we call our function like this 172
milk_mission('store', amount='twenty'). Hitting Shift+Enter, 173
we see that we get an error, and the reason is is that we're trying to assign a value to amount twice - 174
the first time we're assigning the 'store' value to amount because Python is evaluating our arguments 175
by their position and the second time we're setting amount equal to 'twenty' 176
by using the keyword. Okay, cool. 177
So we're coming to the end of this lesson on parameters and arguments and we've covered a lot of material 178
here. 179
This material will sink in more with practice, I promise. 180
And if you've heard any recent buzzwords that make you want to stick needles in your eyes please share 181
them in this lesson's 182
Q&A section. I'd love to hear them. 183
See you in the next lesson.