Functions are fundamental to Python. This lessons shows how to define a function and how to pass values to and from a function.
- [Instructor] Functions are the basic unit of reusable code in Python. So, let's take a look at how they work. Here in Komodo, I've opened a working copy of function.py from Chapter 7 of the exercise files. I want to start down here at the bottom because this is relevant and we have more context now to describe this in more detail. We've already covered conditional expressions and if and blocks and things like that. And so, I just want to describe how this works in a little bit more detail. So, you notice it's an if statement, and the colon is right here, and on the same line we have a function called to main.
And so, with an if statement you may have the code on the same line as long as it's just one line of code. It's normally frowned upon but this is one case where it is commonly done this way. And then we have this conditional expression here which compares using the double equal to a test for equality, for value equality. The special variable name with double underscores on either side against a string literal which says main with double underscores on each side.
So this name variable, this special variable name, will return the name of the current module. So, if this file had been included in another execution unit by the import statement, in other words if somebody had typed import and the name of this file, then this would be running as a module and this name would have the name of the module here, but it's not running as a module. Nobody has imported it and it's running as the main unit of execution.
And so, because of that then this main value is a special value which means "No, this is not important, this is the main file." And so we test for that, and you'll see later on when we start talking about modules how this can come in particularly handy. But, it's often used for this purpose because this calls main, which is defined up above and that's okay because you can call function if it's been defined beforehand, but main then calls kitten which is defined after it and so if we didn't have this and we didn't have a function main but we had function to find after it, we would not be able to call it and so that's called for declarations, and Python doesn't support for declarations.
And so, this is the standard work around for that. And you'll see a lot of, in fact, it's so common that Komodo here, it has code completion for it. If I type "if and two underscores", you notice that there's a number of choices and one of them is that line of code exactly and I can just type in "main", and boom, it code completes that for me. That's just to show you how common this pattern is, you'll see this a lot especially in modules. And so now, if I save this and run it you see it says meow, that's because this if calls main, which is up here, and this main calls kitten, which is a function defined here, and that calls the print function which prints meow.
Now, a function definition looks like this. It has the keyword def and then the name of the function and a pair of parentheses and a colon and then a code block. And so, we've seen that pattern before and that's very, very common. I can, if I want to, I can put a variable or a value or something here, I can put in the number 5 and then down here I can capture that by putting a variable inside the parentheses. So, the parentheses are used for passing parameters, and if I want to, I can make this an f-string and I can print out that n, and save and run, and now that 5 is being printed out by that print statement.
And so, obviously, you know if I change it to a 3, it'll print out a 3 there. So, that's how you pass parameters to a function and obviously, you can put in a coma and pass more than one parameter if you wanted to. And you could capture all of those in the same way in the function. All functions also return a value. So, if instead here, and let me just remove those for now because we're not going to use that. If I wanted to, I could say x equals kitten and then I could print the value of x.
And, when I save and run, it says none because the function is not explicitly returning a value. The value that it does return is the special none value. So, all functions return a value. If you don't specify the value, they will return none. If instead I wanted to return the string, I could do that and now this print statement here on line 6 will print the meow. So, Python has no distinction between a function and a procedure. Some languages do.
Some languages consider a function something that returns a value and a procedure something that does not return a value. In Python, all functions return a value, even if the value is none, so there's no distinction between functions and procedures. Python functions are simple and powerful. And, we'll get into details in the rest of this chapter.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api