Functions
- A function can be thought of as a machine. Like this toaster. We put in bread. And we get out toast. It applies the toasting function to the bread. What if I put in these breakfast pastries? Well, it's not bread, but a toaster can still apply the toasting function to it. Look, it returns toasted breakfast pastries. What if I put in this brick? Or three bricks? This gallon of milk? Hmm, if only this toaster were better programmed to accommodate these inputs. Let's go to the code. We've seen a few functions so far. So print hello world, first one we saw. And print is a function. It has a function name, print. It has these parentheses where you can stick all of your inputs or arguments. This is like bread going into the toaster and the argument for here is hello world. And in Python we can even define our own functions. It's a bit like defining variables. The function name rules are the same as the rules for variable names. Start with a lowercase letter. You can't start with a number, et cetera but we use a special keyword called def which stands for definition. So we can make a function def multiply by three and then we write our parenthesis put any variable names for arguments that we want. And this is just going to take one variable called val for value. Then we add a colon. And just like with the for and while loops, this colon means that we need to indent the next line and start our block of code. So we're going to return three times the value. I'm going to use another keyword here called return. So functions usually return things and what we're returning is three times the value that we passed into the function. So now we can call this function. Multiply by three, four. And the value that gets returned is 12. We could change this a bit and make a function with two arguments. Def multiply val one and val two and we're going to return val one times val two. And then if we call it multiply three, four, again the returned value is 12. Now functions don't always necessarily have to return anything. For instance, we can make a function that mutates or changes a value. So let's define a list A and a function appendFour myList, myList.appendFour. And then we're going to call this on our variable A and then print A. And we see that the number four has been added to the end of this list. This function didn't return anything but it did, you know, perform a function. This is a bit like bread going into the toaster and leaving the toast there for a while for someone to go and collect later. You don't always want it popped back up at you. If we look at the print function, we also see that it doesn't return anything. So let me just copy this and it prints something to the screen, but it's not popping anything back at you. So what if we printed the returned value of the print function? You see it prints none. So the print function actually returns none and none is a special python keyword like null or undefined in many other languages. It doesn't really have a value. It represents the absence of value. It has its own type, the none type. And in general, you want to be really careful when using none. If you think you're getting a value when you try to operate on it like none plus one, you're going to have a really bad time. (toaster dinging) Oh, my milk is done.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。