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.Introduction2 lessons, 11:32
2.Python Building Blocks6 lessons, 1:08:07
3.Controlling the Flow7 lessons, 1:20:10
4.Common Data Structures4 lessons, 46:49
5.Application Structure7 lessons, 1:15:12
6.Collections7 lessons, 46:55
7.File I/O6 lessons, 48:51
8.Networking5 lessons, 43:48
9.Connecting to Network Services3 lessons, 34:27
10.Conclusion1 lesson, 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.