Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Let's explore how to create our own functions
Learn more
-
0:00
[MUSIC]
-
0:04
We've used some predefined functions throughout this course.
-
0:07
And they've proven to be super handy.
-
0:09
You know functions like print and input, we also used len.
-
0:13
Functions are great, they provide a way for
-
0:15
you to group multiple statements together to use later.
-
0:18
They help you avoid duplicating your code.
-
0:21
Now if you ever find yourself writing the same exact code more than once,
-
0:24
it's a symptom that something is likely wrong.
-
0:27
Now we refer to these symptoms as code smell, like hmm,
-
0:31
something about this code smells.
-
0:33
Here, let me help you write some smelly code and
-
0:35
then we'll clean it up with the power of functions.
-
0:38
Getting someone's attention these days is so hard.
-
0:42
In application development, you like have to bounce an icon or make a noise,
-
0:46
[SOUND] to alert them, right?
-
0:48
So one solution that we could do in our textual environment here is to yell in
-
0:53
all upper case.
-
0:54
And I feel like the more exclamation points,
-
0:56
the more likely they're gonna see it.
-
0:58
Let's do that, let's create some text and
-
1:01
then convert it to an upper cased and exclamatory phrase.
-
1:05
So I'm gonna make a new file actually, so let's go here, we'll go to File,
-
1:10
and then New File, and I'm gonna name it notifications.py.
-
1:17
So the first thing that I would love to shout from the rooftop
-
1:21
is some praise for you.
-
1:24
So, I'll assign it to the variable praise and
-
1:27
well say, "You are doing great".
-
1:32
So, we want to uppercase this, right?
-
1:34
And what we'll do is we'll just reassign it, so
-
1:38
we'll say praise = praise.upper.
-
1:42
And that's because strings are immutable and they can't be changed.
-
1:45
So it returns a brand new string,
-
1:47
which we're just going to take our label off the old one and put it on the new one.
-
1:51
Now I think there should be at least as many exclamation marks
-
1:54
as there are characters in the string, to really grab your attention.
-
1:59
So to get the numbers of characters in the string I can use the len function.
-
2:03
The len function expects an object, so we'll pass in our variable.
-
2:07
And then I'm gonna store that in a new variable called number_of_characters.
-
2:16
And I'm gonna call the len function, I'm gonna pass in our string, here we go.
-
2:22
So we wanna create a new value that is a combination of this praise and
-
2:27
the exclamation points.
-
2:28
So let's create a new variable called result.
-
2:33
And we'll make that praise and we'll concatenate the exclamation point.
-
2:38
And we're gonna rely on order of operations here so we'll say,
-
2:43
the exclamation point times the number_of_characters.
-
2:47
And then finally, we'll print that out.
-
2:55
And we'll run python notifications, cuz it's a new file.
-
3:02
There it is, YOU ARE DOING GREAT.
-
3:04
And you're doing great.
-
3:05
That's is some good looking code, and fine smelling too.
-
3:08
You know what, I also like to remind you that you should remember to ask for
-
3:12
help when you need it.
-
3:15
So can we yell that too, please?
-
3:17
I don't see why not, right?
-
3:19
So let's make a new variable, we'll call that advice and
-
3:24
that is, "Don't forget to ask for help".
-
3:28
And then, well I guess I would just copy this code, right?
-
3:36
Copy this code here, and paste it down here.
-
3:41
Guess I would change, I need to change this praise and make that advice.
-
3:46
And I make this advice.
-
3:50
Cuz it's the length of device,
-
3:52
not the length of praise because that's a different variable.
-
3:55
And then we do this, so basically I just copied and pasted that code and
-
4:00
changed this variable name.
-
4:02
That style of coding is called copy pasta, and it's not a good habit to fall into.
-
4:08
And it's really a bad habit, especially if you don't understand what the code
-
4:12
you've copied [LAUGH] is doing, it happens all the time.
-
4:16
So here we go, lets keep going.
-
4:19
You know what, I've got a another piece of advice for
-
4:22
you that I would love you to yell for me.
-
4:24
In coding, there's an acronym that we call dry or D-R-Y, don't repeat yourself.
-
4:32
Let's yell that.
-
4:34
So, I guess that's some more advice, right?
-
4:35
So let's call that advice2, and
-
4:40
we'll say, "Don't Repeat Yourself.
-
4:46
Keep things DRY".
-
4:49
Okay so, I guess I'll copy and pasta this.
-
4:53
We'll put this here and I'll go ahead and do advice2.
-
4:58
Advice2, the length of advice2,
-
5:03
and then we'll do advice2.
-
5:08
And then let's just make sure that this is working.
-
5:11
Okay, It is, but what if I told you that we had like ten more of these to do?
-
5:18
Would you be excited about that?
-
5:20
You know what, actually after seeing this in action, I'd like to make it so
-
5:23
that there are half as many.
-
5:24
This is [LAUGH], this is ridiculous.
-
5:27
That's way too many exclamation points.
-
5:28
So I wanna make only half of those as there are characters, right.
-
5:32
So half of the amount of characters we should do so.
-
5:35
One thing that I can think of doing is if we look we can make it so
-
5:39
that we use the floor division.
-
5:40
That was that the double division where we get an integer.
-
5:43
So we can, if we divide the number of characters by 2 and keep it an integer.
-
5:48
That should be that, and
-
5:49
I'm gonna put parens around here because I want the order of operations to work
-
5:52
correctly.
-
5:52
So let's go ahead, let's run that.
-
5:55
The first one looks great, but the other two don't seem to be.
-
6:00
Rats, I forgot to update those too.
-
6:03
Because my code is duplicated, it means any change that I need to make
-
6:09
I have to change every single place I copied and pasta-ed this code.
-
6:14
Well, that's really smelly right?
-
6:17
So let's clean up the smell, anybody got some Febreze?
-
6:20
[LAUGH] Totally missed product placement opportunity right there.
-
6:24
[LAUGH] I guess functions are definitely an air freshener for
-
6:28
these type of code smells.
-
6:30
So let's create one, shall we?
-
6:33
So the way that you create a function, let's get up here.
-
6:37
The way that you create a function is with the keyword named def,
-
6:42
which is short for define.
-
6:44
And then you give your function a name, let's name it yell.
-
6:48
That seems like exactly what we're doing, right, so we'll say yell.
-
6:51
And then when you're defining a function,
-
6:53
you define what parameters are expected when it's called.
-
6:57
So in this case, we want to accept the text that should be yelled.
-
7:02
Let's go ahead, and we'll call that text.
-
7:04
And then we add a colon, cuz we're gonna start the body of the function.
-
7:08
And you press Enter, and see how it's indented automatically here?
-
7:13
And now, we are in what is called the function body.
-
7:18
This is the code that is run when the yell function is called.
-
7:22
So we basically want to do exactly what we did in our previous code that we were
-
7:26
copying and pasting, right?
-
7:27
So let's just do that one more time, we're gonna copy this code here.
-
7:32
I'm gonna cut it out, when I come in here I'm gonna paste it.
-
7:35
And if you come in front of these lines and
-
7:37
press Tab, you can see that they tab into our body.
-
7:40
So instead of praise, this is now text.
-
7:44
I'm using Shift and the arrow key to do that highlighting.
-
7:50
You get better at those and you forget to say.
-
7:52
So that's how I'm doing that, so text.
-
7:56
That looks good, and
-
7:58
it's really good style to leave at least one space after your function definition.
-
8:03
So that it's clear that it's a function where it ends.
-
8:06
So now, we can go ahead and call that.
-
8:10
So let's do this, we'll get rid of this.
-
8:13
We won't assign it anymore, we'll say yell("You are doing great").
-
8:19
And then we don't need this other code too, let's clean this up.
-
8:21
So we'll say yell("Don't forget to ask for help").
-
8:27
And then finally, let's go call this last one here.
-
8:31
We'll say, yell("Don't Repeat Yourself.
-
8:34
Keep things DRY"), we'll get rid of these.
-
8:38
That looks a lot better, right?
-
8:40
Let's go ahead, let's run it and make sure things are working.
-
8:45
There we go, these lines are even shorter now, great.
-
8:48
So let's review what we've got.
-
8:51
So, we used the def keyword to define a new function named yell.
-
8:58
Yell declares a parameter which is named text.
-
9:01
This is the body of the function.
-
9:03
It's all this code that's indented four spaces by our style definition.
-
9:07
Now, the function body has code that upper cases and concatenates half-ish
-
9:11
the amount of text in the exclamation marks, and then it prints.
-
9:16
We've got a blank line after our function definition,
-
9:19
because we're adhering to good coding style.
-
9:21
It's important, right?
-
9:22
I want you to keep in mind that just defining this function
-
9:25
doesn't run the code.
-
9:27
What it does is it creates a new name of yell that we can call later,
-
9:32
and we can do it multiple times.
-
9:34
So here we're calling the yell method that we just created, and we're passing in
-
9:38
a brand new string that we just created that says "You are doing great".
-
9:42
So that's a brand new string.
-
9:45
And then the values that you push into a function are called arguments.
-
9:49
And since this is the first argument, right, there's only one.
-
9:53
It's the first argument here, and it's called text here.
-
9:57
So basically what this is saying, is text equals you are doing great.
-
10:01
That's what I want you to imagine.
-
10:03
So text equals you are doing great, and
-
10:04
then the rest of the code runs on you are doing great.
-
10:09
It runs through each one of these texts you are doing great, you are doing great.
-
10:13
Prints the result, and then it pops back out to whatever the next line is.
-
10:18
In this case, is another call to the yell function.
-
10:20
And we create a new string that says, don't forget to ask for help.
-
10:23
And imagine again, text equals, don't forget to ask for help.
-
10:27
And then the rest of the function runs, it pops out.
-
10:30
We get to the next one, yell, don't repeat yourself.
-
10:34
So on and so forth, I feel like I'm repeating myself.
-
10:37
Now also if we wanted to change the way that this yell function worked,
-
10:41
we only need to change it in one place.
-
10:44
Like for instance, I think even this too is a little long.
-
10:48
Why don't we do it to a fourth of that?
-
10:50
So if I come up in here,
-
10:52
I can just change this in one place now and all of those functions run.
-
10:57
Functions are pretty handy aren't they?
-
11:00
We've just scratched the surface of how powerful these functions can be.
-
11:04
But, they definitely help you to create nice reusable code.
-
11:08
And try to stay on the lookout for lines of code that are repeated.
-
11:12
Recognize the code smell.
-
11:14
Don't repeat yourself is a great mantra to live by.
-
11:16
It will lead you to write clean, understandable, and maintainable code.
-
11:21
Everyone can agree that repeating what you're saying over and
-
11:25
over is super annoying.
-
11:27
Everyone can agree that repeating what you're saying over and
-
11:30
over is super annoying.
-
11:32
Everyone can agree that repeating what, right.
-
11:35
Keep things DRY everybody.
You need to sign up for Treehouse in order to download course files.
Sign up