paint-brush
Python for Beginners, Part 8: Function Argumentsby@cleverprogrammer
122 reads

Python for Beginners, Part 8: Function Arguments

by Clever ProgrammerMarch 25th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In the series of Python for Beginners - Part 1: How to Download and Install Python. Part 2: Hello World Exercise. Part 3: The Turtle Module. Part 4: The Interactive Shell vs. Script. Part 5: Variables, Part 6: Strings. Part 7: Fun Fun Functions. This video is about how to make a function that accepts arguments, or parameters, or inputs. Those are interchangeable terms. Functions can also accept multiple arguments or parameters.

Company Mentioned

Mention Thumbnail
featured image - Python for Beginners, Part 8: Function Arguments
Clever Programmer HackerNoon profile picture

Let's cover a function that accepts multiple arguments, or parameters, or inputs. Those are interchangeable terms.

In case you missed it, here are the previous parts of the series:

Transcript

0:00 So, in the last video, we left off

0:02 at talking about how to make a function.

0:05 So, we wrapped up our square in a function,

0:09 and that way we were really easily able to use the square,

0:12 and this way we can use the square

0:14 as many times as we want and it's very easy.

0:18 So, let's do something cool.

0:21 I said let's talk about the idea of arguments

0:25 or parameters of a function.

0:27 What we want to be able to do is, let's say that,

0:30 instead of 100, what if we wanted to move

0:32 a different length every single time.

0:35 So, let's say we don't wanna be boring

0:38 and just move, I dunno, 100 steps.

0:42 So let's take a look at it, looks like we don't wanna move,

0:45 oh, I didn't call it anywhere.

0:47 Let's call it again, and I'm gonna remove this line here.

0:51 Okay, let's run it.

0:56 Okay, and let's say we don't wanna be boring

0:57 and make a really small square like this.

0:59 Let's say we wanna maybe go 200 steps, okay?

1:03 So, let's try it; let's go 200, 200, 200, and 200,

1:12 and let's take a look at what that looks like.

1:17 Nice, the square looks better.

1:20 Let's try to tweak it a little bit and let's make it,

1:22 I dunno, 240, 240, 240, 240.

1:34 Hmm, let's see; that looks pretty cool.

1:36 But what if I wanted a turtle to go

1:38 all the way to that edge perfectly,

1:41 and then go down, and then go to the left,

1:43 and then come back up.

1:45 Okay, let's try something; let's try 280, 280, 280, 280,

1:54 and hopefully ... oh, it's still not hitting the exact.

2:00 Maybe let's try 290 or something.

2:03 So, I hope that you're seeing that we have to change

2:05 these values here a lot, right?

2:09 We have to change it every single time.

2:12 And we have to change all four of them,

2:13 in all of those locations, which is kind of annoying.

2:17 So, how can we make it so we don't have to constantly

2:20 change all those values again and again?

2:25 It's kind of really frustrating.

2:27 We want to make it so we can just change it one time

2:30 and it changes it everywhere.

2:33 So, let's say, I dunno, let's call it n.

2:39 Let's make it a variable that's the same everywhere.

2:42 So let's just call it n, and I'm gonna make

2:45 a variable here, and I'm gonna call it n,

2:48 and I'm gonna say go 250, okay?

2:51 So, now, everywhere n is, we have 250, okay?

2:56 So let's try to run this bad boy now.

3:01 Okay, cool, 250.

3:03 Let's make that really small; let's say 50.

3:08 Okay, that was really small.

3:10 That's pretty cool, we were able to use that

3:13 and that made our life really easy.

3:16 But, a lot of the times, we don't wanna change

3:19 the function itself, like, the definition of the function.

3:22 Here, its job is to make a square,

3:25 and that's all it should need to know how to do.

3:28 And we wanna be able to give it something,

3:31 based on which it changes the length of it, right?

3:37 So, like, how far it goes or how big the square is.

3:42 Maybe you can think of it like,

3:46 in English, if you have a definition of a word

3:49 that you look up, once you learn that word, you can use it

3:53 in whatever way you want; it really depends on the context.

3:57 For example, if you wanna say, "Wow, man. You really

4:00 killed it," you're basically saying "You did

4:03 a really good job and you did amazing."

4:05 Or, if you said, "Aww, man. You just really killed that

4:08 thing for me, you know, you really killed it for me.

4:11 I don't even find it fun anymore."

4:13 Now you're using it and the context is basically

4:17 that somebody really made something less enjoy able

4:20 for you or discouraged you in some way.

4:23 So, the word kill, in this case, right, it has

4:27 a completely different meaning, but, based on the context,

4:30 we change it, right?

4:32 Depending on what context we give it,

4:34 we change the word kill.

4:36 But we shouldn't actually go and change the definition

4:38 of the word kill every time we want to use it

4:40 in a different context, okay?

4:42 So, what I'm trying to say here, is that,

4:44 instead of changing the function itself,

4:47 let's do something really cool, let's give it context.

4:51 I'm gonna say a square takes in some input n.

4:55 So, whatever n I give it, it just puts that in here.

4:59 And, instead of calling it n, let's make it more fun

5:02 and call it length.

5:04 So, everywhere I see n, I'm just

5:06 going to replace it with length.

5:09 So, now the cool thing that's gonna happen is,

5:11 if I give it the length of five, everywhere it sees length

5:14 it just replaces it with five.

5:17 If I give it the length of, let's say, I dunno, 200,

5:21 it'll go everywhere and replace everything with 200, okay?

5:25 That way we'll get a longer square.

5:28 So, let's try it.

5:30 If I run this right now, I'm gonna get an error,

5:32 and let's see why.

5:36 Okay, I got an error, and the error says, "TypeError,

5:40 the function square," that's what the parentheses

5:43 afterwards it means, "is missing one required

5:47 positional argument: length."

5:51 We're supposed to give it something, we're supposed

5:54 to give it context, right?

5:56 Like, the word kill, we gave it context

5:59 and it changes the whole meaning of the thing.

6:02 So, here, in square, we're going to give it

6:03 the length, so let's say for length we say 100.

6:07 Okay, now let's see what happens.

6:09 Let's run it, and we get a square

6:12 that it goes 100 in each direction, right?

6:15 So what just happened?

6:17 We passed in the 100 here, which really

6:20 passed in the 100 here, and since there's 100 here,

6:24 it became 100 everywhere else, and,

6:27 so, we got a square that was a size 100.

6:31 And we can effectively do the same thing.

6:33 If I give it a 300, that's probably

6:35 not even gonna fit on the screen.

6:39 Oh, it pretty much does.

6:42 It makes a bigger square, right?

6:45 So, now, the only place I have to change it is really here.

6:49 This thing's job is to, now, just make a square.

6:52 That's all it does.

6:54 And, here, we can tell it how big that square should be.

7:00 So, let's say that we say, "Make a square of 300,"

7:05 and then "Make a square that's, I dunno, 150,"

7:09 and then "Make a square that's 100."

7:13 Let's see what that does; let's see what that looks like.

7:18 Okay, that's 300, that's the 150 and that's the 50.

7:24 Whoa, that was cool, right?

7:27 That kind of ... it seemed like it was about

7:30 to make some kind of design right there.

7:33 Okay, so, hopefully you guys have a little bit more

7:37 of an understanding of how arguments work.

7:40 Just to take it maybe one step further,

7:43 think what if instead of these 90 degrees,

7:45 you wanted to be able to change the degrees.

7:48 Maybe we can go on here and say angle

7:54 and replace everywhere we see 90 with angle.

8:00 And, so now, we have to give our function not one,

8:04 but two arguments, okay?

8:08 So, let's say 300 and 90; so the 300 will replace

8:12 the length part, so everywhere there is length

8:15 it'll replace that with 300, and the 90 will replace

8:20 angle, so everywhere it says angle,

8:23 it'll replace that with 90.

8:24 How cool is that?

8:26 So, let's run this bad boy

8:29 and check out what it looks like.

8:31 We've seen this square before multiple times, 300.

8:34 But now, let's add a twist.

8:36 Let's make this 45 degrees, let's see what happens.

8:42 Oh, oh, I don't even know what it did.

8:46 Let's reduce the length so we can at least

8:48 see what it does, right?

8:51 Let's run it; whoa, that's kind of cool.

8:55 It didn't make a square, it did something weird with it.

9:00 Okay, so, I'm foreshadowing multiple arguments,

9:03 and in the next video we'll take more about it.

9:06 All right, guys, I'll see you there.