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 existent
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 D F 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.
Or this is going to perform an action on a data type.
We use the Colin and then within this block of code we can say print
and that is a function.
If I click Run
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 used 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 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 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 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 and maybe 10 lines of code instead of writing those 10 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.
That's.
Look at that twenty six 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 in 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 a show tree here to the top
well.
If I run this I'll get an air name their name.
Show tree 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.
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 in the next one.