To get started we're going to learn more about functions.
And previously we learned that functions are a really handy way of taking a
complex set of instructions and packaging them together inside a block of code
that has a name given to it.
And when we need all of the lines of code that's packaged inside this
function,
all we have to do at any point later on in our code is just to call the function
by typing its name and then a set of parentheses.
And when this line of code is run,
it's going to search for where this function is defined, which is up here,
and then it's going to execute all the lines of code that are contained within
it. Now it's been a little while since we've seen functions.
So I want you to go ahead and find the starting replit for day 8
which is at this URL
and then fork it to create your own copy. To begin,
I want you to create a new function called greet,
and I want you to give it three lines of print statements,
telling it to do three things. And finally, once you're done,
go ahead and call the function so that it gets executed in the console.
So pause the video and give that a go. All right.
So to create a function, we of course need a def keyword.
And we know it's a keyword, because as soon as we type it,
it changes to the blue color. And after the key word,
we get to give our function a name. So in my case, it's called greet.
Now after the greet, we add a set of parentheses after the name of the function.
Finally,
we add a colon and we enter into the block of code because we're indented over.
Now,
the first thing I'm going to do is to create a print statement that just says
Hello. And then I'm going to say, well,
how do you do? Nobody in England actually says this,
but I always see it in the learn English books, which is quite funny.
I think people will give you a very strange look
if you go to somebody at the bakery and say, well, how do you do? But anyways,
now finally, we're going to add a final print statement,
keeping in line with the Britishness of my function,
going to ask about the weather.
Now this line, on the other hand, is something that you will often hear a British
people say. They're obsessed about the weather,
mostly because it's terrible all the time.
So now that we've created our function,
it's time to actually trigger the function or what we see in programming lingo,
call the function.
And we do that by calling the name of the function and then adding a set of
parentheses. And now once the code reaches line six
it's going to search for this greet function, which it'll find over here
and then it's going to carry out each of the lines of code one by one.
So let's run the code and you should see each of the three print statements
being called. So that's pretty simple.
Notice that every single time I call the greet function,
it's going to do the same thing, right?
It's just going to print out those three lines of code. Now, in reality,
it's very rare that you'll want to repeat the same instructions every single
time when you call a function. It would be nice, wouldn't it,
if we could modify some of the parts of the code inside the function and allow
for a little bit of variation. For example,
it would be nice if we could greet somebody by the name so that it could say,
hello Angela, instead of just, hello. How do you do Angela?
So that it could change each time I call this function.
Maybe next time it will be, how do you do Jack Bauer, et cetera.
So how can we achieve this kind of functionality? Well,
we need to look more closely at these parentheses.
Now what we can do is we can actually add the name of a variable inside those
parentheses to start giving our functions some inputs.
So let's say that there's a variable called something that's going to be passed
to my function. And this something can then be used inside this block of code,
this function.
And I could do something with it. Now in order to actually pass this value
when I call my function, I have to add the data inside the parentheses.
So let's say I decide to pass 123 over, well
in this case, when this line of code gets triggered,
the computer is going to search for where this function is declared,
which is up here, and then it's going to pass over this piece of data,
123, over to this variable called something.
So now, effectively, inside this function called my_function,
we now have a variable called something that's equal to 123
and that can then be used inside the block of code to do something with that
piece of data.
Now it's a bit like plugging in a USB stick into a computer.
If we took a different piece of input,
we would end up with a different file being shown by the computer.
So this means the computer can do something different depending on the input that
we give it. And if we change that input,
then it will receive a different piece of data.
So in addition to a simple function,
we can also create a function that allows for input.
And to do that you saw earlier on the syntax is again the same as before we use
a Def to create the function.
And then let's just give it a slightly different name to differentiate it with
the previous function, greet_with_name.
And then inside the parentheses, instead of leaving it blank,
we get to create the name of the variable that's going to be passed over.
So let's just call it name because that describes the data that's going to be
received. It's going to be the name of the person that I wish to greet.
So now let's add a colon and finally we can create our print statements.
So I'm just going to copy these print statements from above and now,
instead of having these names hard coded in here, I'm going to delete it
and I'm going to replace those names with the value of this variable.
So I'm going to put it in using an fstring like this,
and then I'm going to do the same down here.
Right?
When we actually call this function,
it's also going to be a little bit different from previously on line six.
So now firstly, the name of the function has changed.
But notice how it's expecting some sort of input inside the parentheses.
And it tells me that it wants to receive a name.
It's when we call the function that we actually pass over the piece of data
that's going to be stored inside this variable. Let's give it my own name,
Angela. And now when I hit run,
I want you to predict what's going to happen,
what's actually going to be printed.
So let's go ahead and comment out the previous line of code and now run our code.
And you should see that it says, hello Angela, how do you do Angela.?
So this piece of data has basically been inserted under this variable name
and then it's been put into both of these print statements. Now,
if I go ahead and call this function with a different piece of data,
say a different name,
then it's going to use that piece of data to modify this function
so it does something a little bit different.
So this way we can create one function that carries out some instructions,
but every time we execute it, it gets a modify it a little bit
by changing the inputs.
When we're talking about functions with inputs,
there's two things that we're working with
that's really important to differentiate.
So we know that when we call this function and we pass over this piece of data,
effectively
we're creating a new variable called something and we're setting it to equal this
piece of data that we're passing it. Now in programming lingo,
you'll hear this being referred to as the parameter and this piece of data
being referred to as the argument. Now,
the argument is the actual piece of data
that's going to be passed over to this function when it's being called.
Whereas the parameter is the name of that data
and we use the parameter inside the function to refer to it and to do things
with it. Now very often on the internet when you come across
somebody explaining something
be it on Stack Overflow or in some piece of documentation,
you'll hear them referring to these two types of things,
the parameter and the argument.
So if you need to, come back to this lesson and remind yourself the difference
between those two things.
The parameter is the name of the data that's being passed in,
the argument is the actual value of the data. Now,
sometimes people also get confused on the internet between these two words,
but as long as you know what they means, then you'll be in a good place.