It's time to talk about functions.
This is where things get really interesting because up until now we've seen functions write functions
where things like print, like list bullion.
We even saw the input function to get the input of whatever the user types and those allowed us to perform
actions on our data types.
But the true power comes when we can start creating our own functions.
That's right.
We're not limited to whatever Python gives us.
We're able to create our own functions and use them in our programs.
So let's learn how to do that.
The way we create a function in Python and functions, by the way, exist in all programming languages.
They're very, very important.
If we do de f that lets the python interpreter know that we're about to define a function.
Def is short for define.
So the interpreter is going to say, all right, they're about to define a function.
What's the function going to be?
Well, we can create whatever we want.
We use the same naming case as we do with variables to define our functions.
So let's create a function again.
We'll call it Say Hello.
Just a variable that I created.
But this time it's a function because we use the def keyword now in here and say hello.
We also add the brackets to let the interpreter know that this is something that we're going to take
action on or this is going to perform an action on a data type.
We use the colon and then within this block of code, we can say print.
Hello.
And that is a function.
If I click Run.
Hmm.
Nothing happened.
Why is that?
Well, we've created the say hello function, we've defined it, and now it's living somewhere in memory
on our machine.
However, in order to use a function, remember, just like we use the print function, we have to call
it with the brackets.
So after we define a function, we say Say hello.
And did you notice how my ripple.
As soon as I said say.
Actually gives me the say hello command because I've created it.
And you see this purple box which shows that it's a function?
Well, we now have it available for us to use, just like we had the print function available for us
to use.
So if I do say hello and run it with the brackets and I click run.
I get.
Hello?
How cool is that?
By the way, what happens if I run it without the brackets and I click run?
Nothing happens, because remember, in order for us to take an action, we have to let the interpreter
know.
Hey, I want to run.
Say hello.
Now the reason functions are so powerful is because of the principle that we've talked about.
Right.
The idea of DRI, which stands for Do not repeat yourself.
Functions are really, really useful when you have things that you want to do over and over.
For example, the print function, we've used it a lot.
Imagine if we had to code that ourselves every single time and say what print function does.
Luckily, Python gives us a print because it's such a useful tool.
But if in our program we want to say hello multiple times and you can imagine this actually being a
lot more complicated, maybe ten lines of code.
Instead of writing those ten lines of code over and over, I can just define it as a function and use
it anywhere I want in my program.
For example, remember this where we had a picture and we printed a Christmas tree.
Imagine if we wanted to run this multiple times.
Well, in order to do that, I would copy and paste this code.
And then add it again.
And if I click Run.
I now have two Christmas trees, but that's look at that 26 lines of code.
I just copy and pasted the same thing over and over with a function.
We can do something like this.
I can say.
Define show.
Tree.
Colin and we now have a function.
But remember the indentation in Python, right?
Indentation is important.
We have the semicolon, we're defining a function.
So we have to create that code block inside saying, hey, whatever is indented here, that's part of
this function.
So now that we have the show tree, check this out.
I can say show tree, run it again.
Maybe let's run it three times.
If I click Run.
How cool is that?
I'm able to do the same thing over and over by just calling this function.
And that's the power of functions.
Functions allow us to keep our code dry.
We don't repeat ourselves and reuse things that our machines can do over and over.
And the beauty is that this stays a memory for us.
So Tree now means something to this program because we've created it.
So we have our own custom action that we can take.
Now, what happens if I move?
Show tree here to the top.
Well, if I run this.
I'll get an air name.
Her name.
Show tree is not defined.
Why is that?
Well, because our interpreter goes line by line.
It first says, all right, picture equals to this value.
And then it goes to line 11 and says, Run, show tree.
But we haven't defined show tree yet, so the Python interpreter is going to error out and say, Hey,
I have no idea what show tree is.
What are you talking about?
Instead, with a function, we need to make sure that we define the functions at the beginning.
So that Python interpreter says, All right.
Show tree now means this.
I'm not going to run it.
I'm not going to use it yet.
I'm just going to keep it in memory.
And when I finally come across Show Tree, I'll know that it means something.
I'm going to grab it from memory using show tree and then I'm going to run it using the brackets.
For example, if I do print show tree without anything, without the brackets, if I click run.
You see that I get function show tree at this location.
This is just the location in memory.
This is the bookshelf where we store that show tree function.
Very, very cool.
And functions are an important, powerful concept in programming.
And in the next video, we're going to extend this and explore this a little bit more.
I'll see you in the next one.