FREELessons: 48Length: 7.6 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.5 Creating Functions: Part 1

In this lesson, we are going to discuss creating functions. Functions are repeatable blocks of code that can help us to reduce duplication of code, as well as to share code with other parts of our applications and with others.

2 lessons, 11:32



2.Python Building Blocks
6 lessons, 1:08:07

Introduction to the Interpreter




Standard Input and Formatting

Building a Tip Calculator

3.Controlling the Flow
7 lessons, 1:20:10

Conditional Statements

Looping With For

The Range Function

Looping With While

Creating Functions: Part 1

Creating Functions: Part 2

Building an Average Calculator

4.Common Data Structures
4 lessons, 46:49

Lists, Stacks, and Queues, Oh My!


Iterating Data Structures

Building a Sentence Analyzer

5.Application Structure
7 lessons, 1:15:12






A Special Calculator: Part 1

A Special Calculator: Part 2

7 lessons, 46:55

What Are Comprehensions?

List Comprehensions

Dictionary Comprehensions





7.File I/O
6 lessons, 48:51

File Basics

Reading Entire Files

Navigating a File

Writing to Files

Reading and Writing to Files

Reading and Writing Complex Objects

5 lessons, 43:48

Introducing the Socket

Getting a Remote IP Address

Handling Socket Errors

Create a Socket Server

Create a Socket Client

9.Connecting to Network Services
3 lessons, 34:27

Getting the Current Time With NTP

Getting Websites With HTTP

Downloading Files With FTP

1 lesson, 02:08


3.5 Creating Functions: Part 1

In the last few lessons we have begun to introduce the concept of control flow and being able to kind of modify the flow of your application in a couple of different ways. We've worked on conditionals. We've introduced the if and the else if and the else conditions. And we also begin to talk about some loops, namely the For and While loops. Now, another very common way to control the flow of your application and really kind of encapsulate some functionality into a certain area of your application is through the use of a function. And a function is really just that. It's a way to create a modularized piece of code or a block of code that can be reused over and over again to serve a certain purpose. And it's typically referred to by a name. And in Python, that's going to be fairly similar to other programming languages that you may have used in the past, and if not, then will walk you through it now. So the basic structure of a function is going to be the keyword def or DEF. So we'll go ahead and do that now, we'll say def. So that's going to tell Python that we are about to define a function. And the next thing we need to do is give it a name, and this can be any sort of name you could possibly want. And we'll just call this numbers. And at this point, we have the option to be able to provide some parameters. Provide some arguments to this function, basically inputs to this function. And for now we're just gonna leave it blank. So to leave it blank as far as arguments are concerned, we just to open and close parentheses. That's it. Now just like other loops and conditionals and ways to manage the flow of your application. We're gonna define basically a block of code here. And the way that we denote that is once again through the use of a colon. So once we've done that we have now defined the function. We have defined the name of it, numbers. And the inputs into it which currently right now is nothing, there are no inputs to this function. Now we're gonna drop down to the next line and we're going to indent. Just like everything else when we see this colon here we are going to drop down to the next line and indent. And all of the encapsulated functionality is going to be indented to that same line just to keep things very simple. So now right now, let's just say we wanted to create a simple function that was going to output the numbers, let's say, up to five or something like that, something arbitrary, just so we can see how this is going to work. So we're going to use either for loop or a while loop will work just fine. So in our case, we'll use a while this time, so we'll go ahead and we'll define a number. We'll call it num, and we'll say that this is equal to zero. And then we will create a while loop, and this while loop will continue. While num is less than or let's say to equal to zero in this case, so we'll say less than or equal. And then we'll go ahead and create the body of our while loop which will be simply print num. And then we will go ahead and increment num so we don't get that vicious infinite loop situation. So we'll say, num += 1. So that should be pretty straightforward. We'll create our number = 0. We'll say while (num We will print it. And then we will increment it, and we'll go through the while loop again until we have finish that. And that should end our function. So let's go ahead and save this. Now I'm gonna call this numbers, and we will once again make sure that this is a Python file and we'll save it. So now we'll come into our shell, and I will go ahead and run Python and I will run numbers, and let's see what happens. Well nothing happens. And that is absolutely what we would expect because I haven't actually told anything, or I haven't actually called this function or made it process or made it run, all I did was define it. So that is one of the more important characteristics to remember about a function is that all on its own a function does nothing. You actually have to call that function to actually get it to run. So yes, we have defined this function, we've defined numbers but it hasn't actually been called yet. And the way that we call a function is by coming down a few lines here. And we simply call the function by specifying its name, numbers, and then its parameters, which in this case, there are none, so we just do open closed parentheses. So let's go ahead and save that. We'll come back to our shell and we'll run that, and as you can see here, we started at zero and then we printed the numbers zero through five. We ended our while loop and then that was the end. So now we see how we define a basic function. We use the DEF keyword, we give it a name that we may want. And this mysterious parameter list here or argument list, so how does that work? Well let's start to introduce a little bit of flexibility to this function and figure out yes, we are hard coding the fact that we are running these numbers up to five. But what if we wanted to make that kind of arbitrary and have it passed in and maybe give the user the option to specify how many numbers he or she wanted to print. Well the way that we would do that would be by specifying an argument. Now, the argument list here that we can specify to a function is really limitless. You can pretty much put just about anything you want there and as many as you want but I'm going to tell you this as a little caveat. When you start to define functions that start to take five, six, seven, eight and heaven forbid up to ten parameters. You may wanna think about breaking things up a little bit because ultimately if you're probably passing in that many things you might want to rethink the way that you're structuring your function because you might be having a do a little bit too much, or maybe it's too flexible, so just kind of something to keep in mind. So let's go ahead and see what we want to do here. So let's say we wanted to pass in the limit of how far we wanted this function to go. So this limit is gonna get passed in. In our case, we're gonna pass in a number. But once again, because this is not strongly typed, we could pass an anything in to here. But for this particular instance we're going to assume that the user's going to be nice and that they're gonna pass in a number. And at this point we're gonna start off at number equal to zero. Then we're gonna specify while the number is less than or equal to, and we'll put in here limit. Now, once we've done that we can now go ahead and go through our loop printing our numbers, incrementing them one at a time as we go and then figuring out how far we want to go. So now let's go ahead and start with five again just to see if we get the same results. We'll go ahead and save that. And we'll go ahead and run numbers and yes, we sure do, we get 0 through 5. So let's change this now. Let's change this up to say something like 12. So we can save that now and we'll go ahead and run our application again. As you can see now we get a 0 through 12. Well this is pretty nice, I think this is starting to work out in our favor. But before we end this lesson I want to introduce an interesting kind of thought to you. Now, yes, this is a function and this is in encapsulated piece of functionality that we can use to maybe cut down on redundant code. We could create these nice little functions that can do some sort of calculation for us or do some sort of looping for us and printing out numbers if we should want. But ultimately in Python a function is really a first class citizen. So what does that mean? Well, that also means that I can create instances of a function. And then pass those functions around to different places, maybe as an input to another function or something like that. So let's see how something like that might work. Well let's say I've defined this first function called numbers. And we're just gonna kind of create a silly little example here, just to prove a point. Now, I'm gonna create another function here that is going to be called, in this case we'll say call numbers. And then this is going to be our number function. So what we want to do in this particular function is we want to do one of two things. And actually I'm going to expand this a little bit more. And that this is going to take in a function that's going to call some numbers and that's just because of what I've named it. But I also want to pass into it the limit that we are talking about. So how does that work? Well, so what's ultimately going to happen here is I'm going to call my numb function just as I would any other function. As you can see here I'm going to use this as a function because I'm passing it in here. And so I can specify its parameters and its parameters here are going to be limit. That seems kind of odd, but let's let's just keep going with this thought. So now I have two functions here, I have a simple function called numbers that's going to pronounce the numbers. It's gonna take in the the limit of how high we wanted to go. And then I have another function here called call number, we'll call this call numbers. It's going to take in a function and then it's going to make sure that it passes in a certain limit that we want it to go to. And then it's ultimately going to call this function using the same syntax open and close parentheses. And then passing in that limit, just like this function's signature is up here. So the way that I can do this is I can say my function is going to be equal to numbers. Now the way that we do this is we specify a variable name and we set it equal to a function. We set it equal to a function without the argument list, without the open and closed parentheses. So we're just referring to this particular function. So I've created a variable that is pointing to this particular function. Now I can also say that I want the say upper to be equal to, in this case, we'll say 15, just to make it a little bit different. So now I have a function In the form of a variable. I have an upper limit in the form of a variable. And now I want to call my call_numbers function, this guy right here. And I wannna pass into it my func, which is a function, once again, pointing to my numbers function. And I also wanna pass in the limit which is gonna be upper in this case. And will go ahead and save that. So we've now defined our numbers. We've defined call numbers which is simply going to call the function that we pass into it. Then we create an instance of that numbers function. We get an upper limit and then we call the call_numbers function with those parameters. So let's go ahead and save them. Clear off our shell here. And let's go ahead and run this again. And as you can see it worked. So now this is kind of the process that I'm starting to show you that yes indeed, functions in Python are really first class citizens. You can treat them as any other variable you can create instances of them and you can pass them around. Now This is just kind of something to keep in the back of your mind as this is more of an introductory course on Python. But you can start to think of how complex some applications and some sort of data manipulation and processing might be where you could ultimately getting to some interesting scenarios where passing around functionality could definitely be of a benefit. But I just wanted to give you a very quick example of that. So now that we have the basic understanding of what functions are, in the next lesson I'm going to show you a couple of other interesting little features of functions before we go ahead and jump into another sample application.

Back to the top