Locked lesson.
About this lesson
More about functions and how to use them.
Exercise files
Download this lesson’s related exercise files.
Functions Part 2.docx59.1 KB Functions Part 2 - Solution.docx
59.2 KB
Quick reference
Functions Part 2
In this video we'll look at functions a little more.
When to use
Functions are incredibly important. You'll use them often.
Instructions
Try to use "Return" inside your functions instead of print.
You can asign the output of a function to a variable for future use!
Hints & tips
- Functions should return instead of print
- 00:04 In the last video we looked at functions.
- 00:06 Now we want to continue on and learn a little bit more about them in this video.
- 00:10 So here we have our basic namer function and we're just passing
- 00:14 one simple argument in, printing that to the screen and then exiting out.
- 00:19 And that's fine but you remember in the last video,
- 00:22 I was kind of hesitant about this return guy here.
- 00:24 What I like to do instead of this is
- 00:29 return whatever you want outputed onto the screen.
- 00:33 Now if we save this and run it, nothing happens,
- 00:37 because all we're doing is returning this string.
- 00:40 We're not actually doing anything with it, right?
- 00:42 This is kind of cool, because now we might just want to output this to the screen,
- 00:46 and that's fine.
- 00:47 We could have it like we did before with the print.
- 00:49 But more likely,
- 00:50 you're going to want to do something with the output of a function later on.
- 00:54 Or you might want to manipulate it or do whatever, and so
- 00:57 when you return in like this, that gives you the ability to manipulate it later.
- 01:01 So let's look at a couple of ways we could do that.
- 01:03 We might want to print it to the screen, in which case we can just go print,
- 01:07 just wrap this whole thing in our print function.
- 01:10 And then we get Hello John, so that's kind of cool.
- 01:13 We might want to assign this to a variable, and
- 01:15 then do something with that variable later on or even right now.
- 01:19 So let's create a variable.
- 01:20 Let's call it my_namer and set it equal to this output.
- 01:25 And again, if we save this and run it,
- 01:27 nothing's going to happen because all we've done here is we've run our function.
- 01:31 We've returned this string, and then we've assigned it into this variable.
- 01:36 We haven't actually done anything with it yet.
- 01:37 If we wanted to do something with it, we could print out my_namer, for instance,
- 01:42 that is one easy thing to do, and we get Hello John again.
- 01:45 So when we kind of assign it into a variable,
- 01:48 it gives us all kinds of options.
- 01:50 We could do our handy dandy little for loop, right?
- 01:53 We could go for letter in my_namer print letter.
- 02:01 We save this and run it.
- 02:02 We get this, Hello John, right?
- 02:04 We change this to Tim, save it and run it.
- 02:07 We get Hello Tim.
- 02:08 So I much prefer doing it this way.
- 02:12 Using a return statement to return whatever you want out of the function and
- 02:16 then slap it into a variable that we can do stuff with later on.
- 02:19 Just gives you more control and it's better I think.
- 02:22 So that's what I like to do there.
- 02:24 One thing I do want to note is this guy, this name right here.
- 02:27 This is a variable, obviously.
- 02:30 But the scope of this variable, it only works inside of this function.
- 02:34 So for instance, if I wanted to, outside of the function,
- 02:39 print name, I get an error because, well, you
- 02:45 can't use a local variable that's inside of a function outside of that function.
- 02:50 In this case we haven't actually set name to anything.
- 02:53 So let's change this to John.
- 02:55 So we're going to run our function, it's going to put John in name.
- 02:58 Now let's try and print out name and see, will it print out John?
- 03:01 No, it won't.
- 03:02 We're going to get that same error again, because this is a local variable and
- 03:06 you can't use a local variable from inside of a function outside of that function.
- 03:10 So that's just sort of one little thing to keep in mind.
- 03:13 So here we've got this function.
- 03:14 It's very simple.
- 03:15 We could play around with this.
- 03:17 Now let's change this to x and y and then return x + y.
- 03:23 And let's change the name of this from namer to adder.
- 03:27 All right, so now we could go, let's run adder and let's go 2, 3.
- 03:33 And let's print it out.
- 03:37 And we get 5.
- 03:37 So we could go 3 and 3, and we get 6.
- 03:41 So you can do all kinds of stuff with functions.
- 03:43 They're very, very easy.
- 03:45 You can use strings in there.
- 03:46 You can use numbers in there.
- 03:47 You could do just about anything you want.
- 03:49 And you see here we've got just one line code, right?
- 03:52 You could put dozens of lines,
- 03:54 all of this code inside of here is indented, it's a block.
- 03:58 That's when you see this colon, everything after that is a block so you indent.
- 04:01 And as long as you get towards the end, you slap a return on there,
- 04:05 you're going to be good to go.
- 04:06 So those are functions, very quick introduction, but so very important.
- 04:10 We're going to use them a lot.
- 04:11 You're going to use them forever.
- 04:12 In the next video I want to talk about something called program flow.
Lesson notes are only available for subscribers.