In this lesson you will learn about functions in Python. We have been writing random code for a few lessons now, but we need to learn how to write code that is organized. In other words, we can combine our loose code into smaller chunks of manageable and maintainable code. And those chunks of code are what we call functions. And one great advantage of using functions is that we write it once, then we use it as many times down the road in our application. So we define our function by storing all code into that one chunk. Then we can reuse that function by calling it from anywhere in our application. Actually, the term calling a function can be substituted with the term running a function or invoking a function or executing a function. So basically all mean the same thing. And another way to look at a function is that it is a container that holds a few lines of code that perform a specific task. And that's a key point to note is that functions should only do one thing. For example, we have already worked with and used built in functions in Python, like the print function and the type function. These functions do just one thing each and that's it. The print function just prints whatever we pass in the parentheses. And the type function just takes a variable name and returns its type. So just one specific task per function. I also wanted to clarify a key point in terminology, you often will hear the terms functions and methods. You might also hear them used interchangeably, but there is a slight difference. Functions are what we discussed so far. As for methods, they are also functions but belong to a particular class. Since we didn't discuss classes or objects yet, please understand for now that if a function does not belong to a class, then it's just a function. However, if the function belongs to a class, then we call it a method. They both are defined the same way and they look the same, but we call them either a function or a method, depending if they are standalone or belong to some class. All right, so this is what a function looks like in Python. We start with a reserve keyword called def, which is short for define or definition because we are defining a function followed by a function name. And in this case the function's name is add, then it is followed by a pair of parentheses which can hold items, and these items are called parameters. These parameters are then used in the function's code. Now, some functions have parameters, while some don't need them, so they are optional. And so these parentheses can be empty. Then after the parentheses is followed by a colon, which like we already saw, with loops and conditionals, the colon means that some indented code is coming up and this indented code is the body of the function. And because these lines of code are indented. This signifies to the Python interpreter that they are part of this function. Finally, we have the return statement. The word return in all lowercase is another Python reserved word, just like the def keyword up here. The return statement is used to specify what the function should return. And a function can return any type of value, including strings, integers and even other functions. So if we look at this particular function, we see that its name is add and it takes two parameters NUM one and NUM two. When this function is called, these two values are passed as part of the function call, and we see this function call down here. We call the function by typing its name and pass it any needed arguments? Please note the name difference between arguments and parameters when we pass items in the parentheses during a function call, like this line, we call these items arguments. But when we define a function and include items in the parentheses, like up here, these are then called parameters. So please be aware of this distinction and understand that some people use them interchangeably as well. So we call the function here by its name, then pass it two arguments, a two and a four. Now, the number of arguments must match the number of parameters, and if they don't match, then Python will generate an error. So in this example, the value two will be the value for the NUM one parameter, and value four will be the value for the NUM two parameter. And once this function call is made and the arguments are passed, then the function runs and executes the code in the indented block. And once those lines are run, we come to the return statement, which then returns a value to the function call statement. So this addition value will be returned to this function call down here. Now, not all functions return something to the function call. So in such scenario, we don't need a return statement. Actually, all functions do return something. But when we don't explicitly include a return statement, the Python function will still return something, which is the word none, which is something that happens in the background. In Python, functions that return something are called fruitful, while functions that just run and don't return anything are called fruitless. All right, let me take you to a Jupyter notebook that already has all this code typed up for us. And when I run this code, I. Get the expected output. When I run this code, the Python interpreter starts from the top, and the first thing it encounters is a definition of a function. However, this code will not run until someone calls it first. So the Python interpreter skips executing this group of lines for now. Then it continues down the lines of code and finds this first runnable line of code, which is just a variable that I assigned a value for a function call. So we call this function, which is called Add, and we pass the two arguments a two and a four. This in turn goes to the Definition of this function, stores the values of Two and Four in the NUM one and NUM two parameters, then enters and executes the code block. This runs these two print statements, then creates a new variable called Addition and assigns it the value of NUM one plus NUM Two, which is two plus four. So it yields an answer of six. Then it returns this value using the return statement. So, this value is returned back to the function Call, which is down here. So now this function call is a six and we assign this value to the res variable which we finally printed out on this line. So we get a six in our output. Now, some things that can go wrong with this code is, for example, we don't pass the correct number of arguments. For example, I'll remove the four value from our function call and run this code again. This will generate an error that the Add function is missing one argument which is correct. Also, if I call the function but use an uppercase A instead of the expected lowercase A to match the function's. Name and then run it, I will. Get an error as well. So please be careful again with how you name and call your functions. Now, it's part of Best Programming Practices to include a comment just above the definition of the function to explain what this function does. So instead of going through the code line by line to understand what it does, a one or two line comment that explains this function will certainly help in the future when you or anyone else reviews this code. Also, it's part of Best Programming Practice to leave two blank lines before and after the function. This function does not have any Code before it, but after it. You can see that I left two blank lines between the return statement and the next line of code. Again, these are just best programming practices. So not a requirement, but it's always advisable to follow best Practices. Finally, the function call code must come after the function has been defined. For example, I will put this function call line before the function has been. Defined and. It then I will run. It. And I get an error because I'm calling a function that has not been defined yet because Python reads code sequentially starting from the top. So in this lesson, you learned about functions in Python and stay tuned for the next lesson where you will learn about lists. Thanks for watching.