Instructor: It's time to talk about functions.
This is where things get really interesting.
Because up until now we've seen functions, right?
Functions where things like print, like list, Boolean,
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 def,
that lets the Python interpreter know
that we're about to define a function.
Def is short for define.
So the interpreter is gonna 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, in say_hello,
we also add the brackets to let the interpreter know
that this is something that we're gonna 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('hellllooooo').
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 repple, as soon as I said, say,
actually gives me this, 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, hellllooooo, 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 wanna 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 wanna say hello multiple times,
and you can imagine this actually being a lot
more complicated, 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.
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, def show_tree():
and we now have a function.
But remember the indentation in Python, right?
Indentation is important.
We have the semi-colon.
We're defining a function,
so we have to create that code block inside saying,
hey whatever's 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 show_tree here to the top?
Well, if I run this, I'll get an error, name error.
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 gonna 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 gonna extend this and explore this a little bit more.
I'll see you in the next one.
Bye, bye.