- [Instructor] Most applications, regardless of the programming language they are written in, are broken up into smaller blocks known as functions, and Python is no different. In this example, we're going to take a look at how to work with functions in Python. In the editor, open up functions_start.py. So let's start by defining a function. So functions are defined with the def keyword, and I'll call my function, function 1, and then open close parens.
Now this function doesn't take any arguments, but we'll get to that in a little bit. And then a colon, which indicates the start of the function scope block, and then when I hit return, you'll notice that the next line is indented, so I'll write print, and then inside there I'll give it a string, I am a function. Alright, so a couple things to notice here. In some cases, in other programming languages, this colon will be replaced in say Java or C with curly braces to indicate scope.
That's not how Python works. Python uses a colon, and then the next line is indented, and you can see that this print statement is indented by one, two, three, four spaces, but it's up to you how many spaces you want to indent something. It's not limited to anything, it could be two or three or four, or whatever. So Python uses both the scope def runner which is the colon, and then indentation. Let's add a few more lines of code down at the bottom to exercise this. I'll write func1, and then I'll write print func1, and then I'll write print and then func1 but no parentheses.
Alright, so let's save and let's go to the to tobooger and run this, and I'll show the output window there. So I'll click the little green run button. So in the result, you can see that I am a function got printed, and then the same string followed by the word none, and then finally this string here. So in the first case, the function is being called directly, which executes the contents of the function, causing the print statement to print the string. So that's simple enough. In the second case, the function is also being called inside the print function, so the output is the same as in the first case, but then the outer print statement executes, and since our function doesn't return a value, Python evaluates the return value to be the Python constant of none, and then just prints the string representation of that.
In the last case, the function itself is not being executed at all since we're not including those little parentheses that would call the function. We're just printing the value of the function definition itself, which evaluates to this string that you see here. This just demonstrates that functions themselves are objects that can be passed around to other pieces of Python code. So let's try a couple of additional examples. I'll define a function that takes some arguments, so I'll call that func2, and it'll take arg1 and arg2 as arguments, and I'll just print arg1 followed by a space, followed by arg2, and then I'll define a function named cube which takes one argument, and this one will return a value, and it will return x times x times x.
And now let's add a few more lines to exercise these new functions. So I'll do that down here. So I've got func2 and I'll just call it with values of ten and 20, and then same as before, I'll just do print and then func2 with ten and 20, and then I'll print the result of cube and I'll call it with a value of three. And we comment these guys out. Alright, so let's go ahead and run this.
You can see that on the first line, ten and 20 get printed with a space in between, and that's similar to the first result, and then the second line, we've got ten and 20 being printed again with a value of none, and again that's happening because there's no return value from func2. And then for the cube function, when we print the cube of three, we get the value of 27. So in this case the function does return a value, and that's what gets printed. So let's add another example, and I'll define a function that takes arguments and has a default value for one of them.
So I'll call this one power, and it takes a number and then an x, and I'm going to write x=1. And then inside the function for power, I'll say result is equal to one. Now we haven't covered loops yet so just bear with me. So I'll say of for i in range(x) and another scope result equals result times number. And then we'll return the result.
So it basically takes a number and raises it to the given power, okay. And in the function definition, I've got x is equal to one, so this assigns a default value for that argument. Let's go ahead and add some lines to exercise this. I'll write print power of two, and then I'll write print power of two comma three, and let me comment these out. So here I'm calling the function power, but I'm not giving it a value for x, so x is going to default to one.
The next indication calls the function with none is equal to two, and the power is equal to three, so let's run this. And you can see that two raised to the first power is two, and then two to the third power is eight. Alright, now let's add one more invocation test. So I'll scroll down here, and I'll write print power, and in this case I'll write x=3, and the num=2. I'm reversing the order in which the arguments are called.
So Python lets you call functions with their named parameters along with their value, and when you do this, the Python interpreter figures out which arguments to supply the values to. You don't have to call the function with the arguments in a particular order, if you simply supply the names along with the values. So let's run this again. And sure enough, you can see that the results are the same. Two to the third power is, in fact, eight in both cases. We've got one more example to look at.
I'm going to define a function, this time I'll call it multi_add, and the arguments are going to be a variable argument list. So I'm going to write star args, and then inside the function I'll define a variable that defaults to zero, and then for x in args, I'll write result equals result plus x, and then I'll return the result. So what I've done here is define a function called multi_add, and the star character means I can pass in a variable number of arguments.
The function then loops over each argument and adds them all to a running total, which is then returned. So let's add a line to try this out, and for multi_add, I'll just simply give it some arguments like four, five, ten, four. So now I'll save and I'll run this, and you can see that the result is 23. So if I change the function call to include another parameter, in this case I'll give it a ten up front, then I'll run it again, and now the answer is 33.
You can combine a variable argument list with a set of formal arguments, but just keep in mind that the variable argument list always has to be the last parameter. Alright, so that concludes a quick look at functions. Let's move on to the next lesson.
- Installing Python
- Choosing an editor or IDE
- Working with variables and expressions
- Writing loops
- Using the date, time, and datetime classes
- Reading and writing files
- Fetching internet data
- Parsing and processing HTML