What are functions? We discuss how functions work and allow you to use clean, reusable code. We do this while visualizing our code through the Turtle library in python.In case you missed it, here are the previous parts of the series:
Transcript
0:00 Okay so this part now, naturally leads us
0:04 to fun fun functions, alright.
0:07 So let's talk about that.
0:09 Alright, so what we have done so far has been really cool.
0:16 We figured out how to draw a square
0:19 and we figured out how to write it as a Python script,
0:22 so when we run it, it all runs at the same time
0:25 instead of running it here one by one.
0:27 We also learned about variables and strings
0:30 so in here the thing that's a variable is my_turtle,
0:35 and then we keep using that variable over and over again.
0:40 Let me ask you a simple question.
0:41 In this program right here,
0:42 is there a single string that we're using any where?
0:47 Yes or no, what do you think, three, two, one...
0:50 Hmm... there isn't any strings that we're using here.
0:53 What we are using here are integers,
0:57 and integers are just whole numbers
1:00 from negative infinity to positive infinity.
1:03 So you have negative, you could have 100, 90,
1:06 but if you have 90.2, that's not an integer,
1:12 that's something else, which we will discuss.
1:15 But up to this point, I think that we are...
1:21 This naturally leads us to functions,
1:24 so let's talk about that.
1:26 Notice that when this part of the code runs, what it does.
1:32 This part of the code I want you
1:33 to just kind of memorize it for now,
1:35 you're not gonna really understand what it means
1:37 until we get to object-oriented programming.
1:40 But I want you to just focus on this part,
1:43 and what does this part do?
1:45 So let me just simply run the code
1:46 and let's see what that part does once we run it.
1:51 And it goes and it draws a square.
1:55 So this part of the code draws the square.
2:03 Okay, that's cool, what if we wanted to make...
2:09 Whenever we want to make a square it seems like
2:14 we would have to copy this code and write it again.
2:16 So for example, let's say my_turtle.forward(100)
2:24 and let's see what happens.
2:26 I'm gonna save it and I'm gonna run it.
2:31 Okay so I'm gonna move it forward.
2:34 And now I'm gonna try to draw another square,
2:38 so I'm just gonna copy paste that code.
2:46 Oh cool, it kind of looks like glasses or something.
2:49 So let's go back.
2:52 So this is a square
2:56 and this part is a square.
3:01 So every time you have to draw a square
3:03 you have to copy this whole bunch of code
3:06 and write it again and again every time.
3:10 Well think about this, back when we were using variables,
3:15 did we have to remember all of these numbers?
3:19 No, we just stored it in X.
3:21 So I'm gonna teach you guys a trick
3:23 where you can take this entire block of code
3:26 and kind of give it a name.
3:28 Like a variable name, almost.
3:30 But it's a little bit different
3:31 and when you assign something to a block of code,
3:34 it's called, you put it in a function.
3:38 Okay so here, it's all gonna make sense.
3:44 So I'm gonna remove this up at the top.
3:48 Oh, quick thing, just in case you guys
3:50 don't know what this is.
3:51 This is called a comment.
3:53 Anything that you write with a hashtag in front of it,
3:56 you're telling Python to ignore that part.
4:00 I don't want you guys to get confused by that.
4:03 So for example if I write #HI here, it'll just ignore it.
4:07 But if I write HI here, it'll give me an error or something.
4:11 So hashtag means I'm putting a comment
4:13 and it's only for humans to read.
4:16 So now let's turn it into a function.
4:18 So I'm gonna say D-E-F, that means define.
4:22 And we're gonna define some function.
4:23 So what does this thing do?
4:26 It draws a square, right?
4:28 So I think it would maybe make sense
4:30 if we called this function a square.
4:33 And whenever you create a function
4:35 you have to put open close parentheses after it,
4:38 just like that.
4:40 And put a colon, okay?
4:42 And now everything that you want
4:44 to make part of that function, you have to indent it.
4:50 So what I'm gonna do is go here, hit tab,
4:52 go here hit tab, go here hit tab,
4:56 and then you can also just highlight
4:58 that part of the code that you want to move
5:00 and just hit tab all at once, it'll move it for you.
5:04 Now let's see what happens when we run this code.
5:11 So I'm using the shortcut to run the code.
5:15 Hm, that's weird, it's not really moving anywhere.
5:19 Think to yourself, why is that happening.
5:22 Okay, let me tell you this.
5:24 If I wanted Bob, or let's say X, to be this number.
5:33 And I wanted to make X spit out that number,
5:37 print that number out to the screen.
5:39 Well if I define X to be something,
5:42 that's not the part that does the spitting out.
5:44 That's just me defining what X is.
5:47 But the part that does the spitting out
5:49 is when I actually say X and hit enter
5:51 and it does something.
5:52 So if I do banana is yellow,
5:58 then if I want to say the yellow part,
6:01 I would actually have to say banana,
6:03 then it spits out yellow.
6:06 Just like that, me defining a function
6:09 doesn't call the function,
6:11 doesn't actually use the function.
6:14 So I actually have to call the function
6:15 or use the function to make it do something
6:18 and make it spit it out, pretty much.
6:22 So here I'm gonna say square.
6:27 So if you were born and someone gave you the name Jimmy,
6:31 that's them giving you a name.
6:33 They're not calling you Jimmy,
6:34 they're not telling you to come over, Jimmy,
6:36 they're just giving you a name,
6:38 attaching that label onto you.
6:40 Then later when they want to call you, they go hey Jimmy.
6:43 And you're like what's up.
6:46 They're like Jimmy, come here.
6:47 And then you respond to them.
6:49 So just like that, we give this function a name: square.
6:54 And here we're calling it.
6:56 So now let's see what happens.
6:59 So I'm going to run this.
7:07 Look at that.
7:08 It made a square.
7:09 Well what does that mean?
7:11 What if we want it to do that same thing,
7:13 where we moved forward 100
7:15 and we wanted to make another square.
7:17 So let's say I put this line move forward 100.
7:22 And then I want to make another square,
7:25 how would I make another square?
7:27 Should I copy all this code and then paste it right here?
7:33 No, the trick is that I just call that function again,
7:37 that makes a square.
7:39 So let me run it.
7:47 Okay, how cool is that?
7:49 So why do we create functions?
7:51 Hopefully it's clear to you.
7:53 We create a function so we can just
7:56 reuse that code over and over again
7:58 without having to write all of that stuff again
8:02 and again and again.
8:05 To make it a little bit cooler, another thing I'll show you
8:09 about functions in the next video
8:11 is how we can give arguments to functions, or parameters.
8:16 So until then, I'll see you in the next video.