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 Bolian, 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, exists in all programming languages.
They're very, very important.
If we do D.F., that lets the Python interpreter know that we're about to define a function.
DSF 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 name in 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 that, Colin, and then within this block of code we can say print.
Is a function if I click run.
Hmm, nothing happened.
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 Reppel as soon as I said, say.
Actually gives me the say hello, comen, 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 dry, 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 brain 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 10 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 added again, and if I click run.
I now have two Christmas trees, but let's look at that, twenty six lines of code, I just copy and
paste it the same thing over and over with a function.
We can do something like this.
I can say define show.
Tre.
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 show 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, air name, show three is not defined.
Why is that?
Well, because our interpreter goes line by line at first, says, all right, picture equals to this
value.
And then it goes to line eleven and says, run, show, tree.
But we haven't defined show tree yet.
So the python interpreter is going to air 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, Shokri, at this location, this is just the location and 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 in the next one.