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.6 Creating Functions: Part 2

With a solid understanding of the basics of functions, in this lesson we will cover some of the cool features of Python functions that make them even more flexible.

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.6 Creating Functions: Part 2

Now continuing on with functions, I wanna show you a little bit more of the functionality that you can use in this world of functions. So the next thing that I wanna do is I'm gonna create a very simple function. We're gonna define this function and we're gonna call it sum. Now sum is going to be a very simple function. It's going to take in a couple of numbers, and we are going to add them together to get a result. So let's see how we should do something like that. So let's say that we're going to take our argument list here and then we're gonna have some sort of body. So let's say that this particular version of this function, because it's fairly simple is going to take in two numbers. So we're going to have the first number, and we're going to have the second numbers. So, we're going to make the assumption at this point that these two variables are arguments into our function are gonna be integer values. So once we have done that we can now take these numbers in. And then we can start to do something with them in this case we're just going to add them together. And then we want to get a result from this. So how do we do that? We didn't talk about that much in the last lesson, we just had our last function process some data and then be done. It went through a loop, printed out from zero up to a bunch of numbers and then just finished the function. So in this case we actually wanna get a result from the function, so how do we do that? Well, let's go ahead and start to work on the body of our function. In this case, remember we said that the functions purpose is to add two numbers together. So we'll say that our total is going to be equal to first plus second. Now once we've done that, we could just leave it this way and be done and say printed out. But what if within the flow of my application I needed to get that sum and then do something else with it later on down the line, so how would we do that? In order to get a result out of our function, we need to introduce a new keyword, and that's going to be return. Now, return is going to take whatever data comes after it and spit it back out of our function and allow us to assign this return value to some sort of variable somewhere else, and then ultimately use that. So we're going to return total, and they we'll come down here a little bit further, and we'll go ahead and call this some function. So we'll say sum, and we wanna pass in a couple numbers, we'll say five and six. But now instead of just running this, because if we were to run this and actually I'll do this as an example, we'll just call this add.py. And we will come over here and we will run this. So we'll say python add. And as you can see nothing actually happens. It doesn't provide any data or any sort of visualization that it's completed other than we just get our prompt again. And the reason for that is because we're returning a value, but we're not actually doing anything with it. So let's go ahead and specify a variable here, so we'll say result=sum(5, 6). And then once we've come out of here, we'll go ahead and print this out, we'll say print(The result is, result). Just like that, with our print function like we've shown before. So now we'll come in here and we'll run our application again, and as you can see here, the result is 11. So now just play introducing this keyword here, return, we're now able to spit out some sort of value out to the outside world from our function and assign it to some sort of variable and then be able to do something with it later on. Okay, well that's nice but what would happen, let's say, if somebody was calling our function and didn't pass it any values? Let's go ahead and say that, so in our function we're assuming we're gonna get a first and a second. But what happens if the person calling our function doesn't provide anything? Let's go ahead and run that, and as you see here we're going to get an error. And it's going to say that the sum function is missing two required positional arguments, first and second. So what do these things mean, Required and Positional arguments? Well, by default all arguments that you provide in this list for a function are required. Now I say by default because there is a way around that, and it's a very interesting thing to keep in mind when you're creating functions like this. If you wanna be able to provide some sort of default functionality. So how do we provide default functionality? Well, the way that we do that is by, within our argument list here, we can specify default values for these arguments by simply specifying an equal sign. So in this case we could say the default value for second is going to be zero. Now let's just leave it at that. So let's go ahead and save this. And if we were to clear this off and run or application again, the error we get is similar, but it is now changed a little bit. Because now it says the sum function is missing one required positional argument first, as you can see here. By adding this = 0 onto the end of one of these arguments, it's now become optional, because if it doesn't get passed in, it's going to use this default value. So let's do the same thing with first, we'll say first = 0 and we'll go ahead and save that. And now we'll go ahead and clear off our shells and we'll run this again and we see that the result is 0. Because we did not pass in any values it used the default versions of these arguments within our list for our function here. And it just simply added zero plus zero return zero, the result is zero, and now, print is going to return the result of zero, so that's pretty nice. But what if something happens in the middle there? What if we want to provide one of the values, but maybe not necessarily the second value? Well the way that we do that is by using something called Keyword Arguments. And a keyword argument is simply a way for us to specify which one of these arguments, or both, if we wanted to specify within our calling function here, and the way that we do this by specifying the name. So let's say I only wanted to specify second, but wanted to take the default for first. Well, the way that I could do that is I could say within my function call, I could specify the keyword name or the keyword argument name, which is second and I'll specify that as being five. So now, I can save this. I'll clear off my screen and I'll run this, and as you can see now the result has become five. Okay, now let's also try that for first. So we can say now first = 2. Now we're going to pass in both values again, so let's run that. Oh, we see the result is seven. I could get rid of second here. And I could save that, and I could run this again. The result is two. That's exactly what I would expect. Now what would happen again if I were to say put in values here again. Let's say two and three. Let's save that. Go and run. We once again get the result of five, just as we would expect. But what would happen if I were to come in here and say okay, I want this first to be equal to two, and then the second one will be able to three. Let's save that, and there we have a problem. And this is where we start to run into just little gotchas here and there, things you're gonna have to make mental notes of or even sticky notes of. As you start to journey into the complex world of some of the options you have within Python. When it comes to optional parameters in functions, as soon as you begin to specify the name of a function or a keyword argument into a function, you have to continue providing all the function or the keyword arguments after that. So that means I could get rid of first here and I could say two and then I could say second equals to three. And I can save that and if I were to run that it's going to work. And that's because as soon as I provide a keyword argument to one of my functions. I have to continue using keyword arguments for all of the arguments to that function from that point on. So if you start with a keyword argument for your function you have to continue all the way down. And I think that's probably a good amount for you to really get started into the world of functions. And now that we have some tricks up our sleeve, namely if and if else, and for a while, and being able to create some functions, we now have a lot of power, a lot of tools in our tool belt. So in the next lesson we're going to walk through the process of creating a simple application, some simple functionality that we can reuse over and over again using just the things that we've learned in this section.

Back to the top