Locked lesson.
About this lesson
What are functions and how to use them.
Exercise files
Download this lesson’s related exercise files.
Functions Part 1.docx59.1 KB Functions Part 1 - Solution.docx
59.6 KB
Quick reference
Functions Part 1
A function is like a program inside of your program.
When to use
Use them to break apart your code into reusable parts.
Instructions
To create a function, just name it and pass some parameters:
def namer(first_name):
return ("Hello %s" % first_name)
To run a function, just call it:
print(namer("John"))
Hints & tips
- A function is a program inside of your program
- Functions only run when you call them
- 00:04 In this video, I want to start to look at functions and
- 00:07 functions are pretty important in any programming language.
- 00:10 And what a function is basically is,
- 00:12 it's a little program that runs inside of your program.
- 00:15 We've already seen functions quite a bit throughout this course and
- 00:19 you didn't probably even know that they were functions.
- 00:21 For instance, print, that's a function.
- 00:25 So we can pass in Hello World.
- 00:29 If we save this and run it, it prints out Hello World.
- 00:32 So this is a print function and we're passing an argument into the function and
- 00:38 then it's doing whatever that function is supposed to do.
- 00:41 In this case, it's printing out whatever we put in these parenthesis to the screen.
- 00:46 That's how all functions look, they have a name, and then these little parentheses,
- 00:50 and then inside the parentheses, you pass some sort of information.
- 00:54 And what you pass depends on what the function is supposed to be.
- 00:57 Like I said, this is a function, this is a built in function,
- 01:00 Python has hundreds of built in functions, we've used some, you'll use them forever.
- 01:04 But in this video, I want to look at creating our own functions,
- 01:07 writing our own functions.
- 01:08 And it's very simple to do that.
- 01:10 So let's create a function.
- 01:11 And to do that, you type def stands for define.
- 01:14 We're going to define a function, and let's call it namer.
- 01:17 And then put our two little parentheses there and
- 01:20 this is an argument we want to put into the function.
- 01:24 So we need a variable of some sort, let's call it name.
- 01:27 Inside of our function let's say, we want to print out Hello name, wrap this.
- 01:36 And then let's do this like we've done before, name.
- 01:40 So here is the name, and this is that same name,
- 01:43 we're going to swap the name into this thing right there.
- 01:46 We've seen how to do this before.
- 01:47 So then that's it, we're done.
- 01:49 Now, you're supposed to put return at the end of this.
- 01:51 Not going to do at this time, we'll talk about that in just a second.
- 01:56 So, if we save this now and run it, you'll notice that absolutely nothing
- 02:01 happens because that's the thing about functions.
- 02:03 Once you create them, they're just created, to actually use them,
- 02:06 you have to call them and that's what makes them different than regular code.
- 02:10 Up until now, all the code we've written, as soon as we run the program, that code
- 02:14 gets executed and it does whatever it's supposed to do, not so with functions.
- 02:18 They only get executed if you call them specifically.
- 02:21 So this is great for, like, program control, flow control of your program,
- 02:25 and stuff like that.
- 02:26 How do you call a function?
- 02:28 How do you use a function?
- 02:29 Well, it's really simple.
- 02:30 All you do is type the name of it and then,
- 02:33 passing whatever parameters you want, so I'm going to pass in John.
- 02:37 If we save this and run it, we get Hello John.
- 02:40 So what's happening is, our program starts at the top, comes down, it sees this
- 02:44 function, it reads it into memory and it remembers it, but it doesn't do anything.
- 02:48 And then when we come down here, it says okay, now it's time to use this function.
- 02:52 It passes this argument, John into our function right here and so
- 02:57 name becomes John, and then it prints out name.
- 03:00 We could change this to Tim, save and run it, Hello Tim.
- 03:05 So, just that simple, functions are supposed to return items, right?
- 03:10 So if we save this and run it now, we still get the same Hello Tim.
- 03:15 Technically, this is the way you're supposed to do it, but
- 03:17 we're going to see in the next video that how you use the return statement can
- 03:20 sometimes change things around.
- 03:22 So we'll get into that in just a minute, but this is the basic setup of a function,
- 03:26 you can pass as many parameters in here as you want.
- 03:28 We could go, first_name, last_name.
- 03:33 Here, we could go Tim, Elder,
- 03:37 let's change this to John, and
- 03:41 let's change this to first_name and last_name.
- 03:47 Let me save this and run it, Hello John, Hello Elder.
- 03:50 As long as the number of arguments here match up to the number of arguments that
- 03:55 you pass in, you're good to go.
- 03:57 If we, for instance left one of these off and saved it, boom,
- 04:01 we get an error because we're only passing one thing in but
- 04:04 our function requires two parameters, two arguments.
- 04:07 So that's one thing to keep in mind.
- 04:09 Those are functions.
- 04:09 This is a very quick introduction to functions.
- 04:12 In the next video, we're going to look at them in a little more detail and
- 04:15 go on from there.
Lesson notes are only available for subscribers.