Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Functions in Python are the primary unit of reusable code, even in objects. Object methods are simply functions that are properties of the objects. So let's look at how we define a function. We'll make a working copy of functions .py, call it functions-working.py and we'll go ahead and open that up and here we have a very simple function. It's called testfunc and it has one line of code in it.
So if we run this, we see that that function gets run because it's getting called from main. Now, a few things to notice in this file. First of all, main is getting called down here. So main is a function just like any other function and it's simply getting called from the end of the file. The reason it's getting called from the end of the file is so that it can use things that are defined after it, like for instance, testfunc. If instead of calling main from the end of the file, if there is simply a call to testfunc from the top.
that would not work. We'll save that and run it and you see we get NameError because name testfunc is not defined at the point where it was called. So in order for that to be defined first, we put it in a function and we call that function from the end. And so that's one of the reasons that we have this pattern at the end of the file that calls main and then main is defined at the top. It's also worth noting that we have to have some content in the suite after the colon.
So if this were like this, def testfunc(): like that and save it and run it, we're going to get an error, IndentationError. So it thinks that I have intended to put this inside the function, which would not work, because there is no body of the function. So there has to be something there. So if you're looking for a way to simply have a stub, if you're outlining some code and you just want to have a function definition without any body, there is the pass statement, which is essentially a NOOP.
Save this and run it and now it works just fine, there's nothing happens, but pass is legal content that doesn't actually do anything. So you'll see that sometimes when people are working with things or explaining things or creating a code in Python. It's a placeholder. It's a stub. It's just there to make it syntactically correct but it doesn't actually do anything. If I want to pass an argument to my function, I can do that by putting an argument name here, call it number, and then I can print that out over here.
So if I save this and run it, you see we have This is a test function 42. So the number is getting passed in during the function call and the number is getting picked up in this variable, which is defined in the function definition, and then I can use that in the body of the function itself. I could have another number and I could even have one more and I could pass those and I could use them down here.
I save this and run it. Then we get all of those numbers. Because these are defined, they must be passed. If I don't pass all of them and I try to run that, you see I get a TypeError, testfunc() takes exactly 3 positional arguments (1 given). You might want to have some optional arguments and you can accomplish this by giving them default values. You give them default values by assigning values in the function definition.
So now these arguments are optional and yet they'll still work inside the function. So if I save that and I run it, you see I've got the 43 and the 75 from in here and the 42 from there, but if I put a different number up here for the second one and I save that and I run it, then we get that value. Then the default value is not used and instead the value passed is used. The reason for this is that all the function arguments must have values assigned, so that when that function is called, what happens is predictable, not just to you the programmer but also to the compiler.
And so they all must be initialized in one way or another. If you don't want it to have a default value but you still want the parameter to be optional, then you'll assign it explicitly the value None. And now when I save this and I run it, I'll have the value 16 there because I passed it, but now I cannot have that there and I can save it and I run it and I get the value None. None is a special value that you can actually test for. I can say, if another None, it's a singleton object and so identity is a good way to test for it. print. Or I can assign something to it. I can say another = 112 and now when I save and I run, I get 112 here because I tested for None and that test was successful. But if I put something in here, 61, let's save that and run it, then we have the 61.
So function arguments must be initialized in one way or another. Either they are going to be required or, if you want them to be optional, you have to assign them default values. The default value can be none and you can test for none, but it must be explicitly assigned. So that's fundamentally how you define a function and how you pass the arguments in Python.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98414 Viewers
61 Video lessons · 85697 Viewers
71 Video lessons · 69624 Viewers
56 Video lessons · 101948 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.