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 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 the 'def' keyword,
and we know it's a keyword because as soon as we type it it changes the color.
And after the keyword, 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?"
Now, 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 very strange looks,
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, I'm going to ask about the weather.
Now, this line, on the other hand, is something that you will often hear 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 say 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 6, 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'd be nice if we could greet somebody by their name so they 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'll 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 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 f-string, like this.
And then I'm going to do the same down here.
When we actually call this function, it's also going to be a little bit different from previously on
Line 6.
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, we get to modify it a little bit by changing the input.
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 in.
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 StackOverflow
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 mean, then you'll be in a good place.