Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
FREELessons:48Length:7.6 hours

Next lesson playing in 5 seconds

Cancel
  • 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.

1.Introduction
2 lessons, 11:32

1.1
Introduction
02:03

1.2
Prerequisites
09:29

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

2.1
Introduction to the Interpreter
09:51

2.2
Numbers
10:53

2.3
Strings
14:36

2.4
Lists
11:33

2.5
Standard Input and Formatting
12:00

2.6
Building a Tip Calculator
09:14

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

3.1
Conditional Statements
12:47

3.2
Looping With For
09:36

3.3
The Range Function
10:41

3.4
Looping With While
13:06

3.5
Creating Functions: Part 1
11:55

3.6
Creating Functions: Part 2
08:49

3.7
Building an Average Calculator
13:16

4.Common Data Structures
4 lessons, 46:49

4.1
Lists, Stacks, and Queues, Oh My!
11:45

4.2
Dictionaries
10:10

4.3
Iterating Data Structures
09:41

4.4
Building a Sentence Analyzer
15:13

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

5.1
Modules
09:08

5.2
Packages
11:24

5.3
Classes
09:53

5.4
Attributes
09:34

5.5
Methods
12:01

5.6
A Special Calculator: Part 1
13:36

5.7
A Special Calculator: Part 2
09:36

6.Collections
7 lessons, 46:55

6.1
What Are Comprehensions?
06:32

6.2
List Comprehensions
06:08

6.3
Dictionary Comprehensions
06:38

6.4
Map
05:45

6.5
Filter
06:31

6.6
Lambdas
05:21

6.7
Generators
10:00

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

7.1
File Basics
06:50

7.2
Reading Entire Files
07:49

7.3
Navigating a File
08:32

7.4
Writing to Files
07:22

7.5
Reading and Writing to Files
09:15

7.6
Reading and Writing Complex Objects
09:03

8.Networking
5 lessons, 43:48

8.1
Introducing the Socket
04:39

8.2
Getting a Remote IP Address
06:42

8.3
Handling Socket Errors
07:58

8.4
Create a Socket Server
16:04

8.5
Create a Socket Client
08:25

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

9.1
Getting the Current Time With NTP
10:38

9.2
Getting Websites With HTTP
12:57

9.3
Downloading Files With FTP
10:52

10.Conclusion
1 lesson, 02:08

10.1
Goodbye
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