 In this video we're going to continue to learn about functions and pythons, in particular in this video we're going to learn how we can write our own functions. So let's go ahead and create a new file and let's call it user defined functions. That's the technical term for a function that we define on our own. And let's briefly recap what we have learned before. So we are talking about functions in the generic sense and we have introduced the concept of a caller bone which is any object and we have learned by now that everything is an object in pythons. So any object that can be called and called is just another word for executed. And we have seen two examples before and the two examples were first of all built in functions. So function objects that are already in memory once we start a new python process. And then also we talked about constructors which we can call like functions, but they are not really functions. So let's maybe also give one example each. So for example we have the sum built in function here and for example for a constructor we have the in constructor for example which takes any object, any numerically parsable object and casts it as an integer object. Okay, so now comes the new idea. Namely we want to define our own functions. That's the technical term defining so that's why we call this subtitle function definition. And let's go back to our very first example in this course and the example of course you remember it it goes like this, find the average of all even numbers in a list. And I gave you a list to work with so let's do that here as well. In previous videos we called this list numbers but for now I will call it integers and you will understand in the next video when we talk about function scopes why I do so. Okay, so let's go ahead and define the functions just for the example, so this would be 12 numbers from 1 through 12 in no particular order. So I always try to keep the same example so that you're not confused. So that's a list of numbers and previously how we went ahead and solved the problem goes as follows. And that was the second way of how to solve the problem which was in the video talking about best practices and a little bit about PEP 8, that's the other video where we learned about this. So what we do is we write a list comprehension to create a new list object out of the existing one that only keeps the even numbers. So this could be written like this. We write n for n in integers if n modulo divided by 2 has no rest so that's the definition of an even number. And then we assigned this list, this new list to a variable called events to make the program easy to read that is why we called the previous video where we introduced this idea best practices so you want to keep your variable and your programs readable. And then we go ahead and we use two built-in functions namely the sum-built-in function and the lend-built-in function to calculate the average. And if you want to make this a little bit nicer we would also go ahead and assign the result to a variable called average and then write the average variable towards the end of the code cell so we can see the result and now this is a very readable program. So what is the downside of writing code like this in a Jupyter Notebook? Well, the problem is, let's say I give you a different list of numbers and I ask you to calculate the average of the events of a different list of numbers. Then what you could do is you could go ahead and you could go back to the first code cell you could change the numbers you could copy-paste over the new list that I give you and then you would execute the first cell again to update integers and then you would execute the second cell again to calculate the new average. However, that is not a good way so whenever we want a piece of code to be repeatable then it is good to put it inside a function because functions, as we see with the built-in functions are just there to be re-executed so the whole idea of a function is to reuse the code that is in it. So let's learn about how we do that. So make this code repeatable with a function. So let's go ahead and define that's the dev statement so let's define a new function. Every function must have a name however there will be it's actually not 100% the case but just as with built-in functions a function could have a name and because we are talking about a beginners course here we will give every function for now a name but there will be ways in the future of how we can create functions without a name but let's go ahead and name the function average event so a good name for a function is always a name that kind of tells us what the function does average event so that's what we do what the function does the function takes parameters as input so we write parentheses these parentheses are not the call operator to call the function but these are just parentheses that define the parameters the function takes and this function for now will only take one parameter and let's call it for now integers we will change that in a future video as well when we learn about new concepts and then we enter the line with a codon and ending a line with a codon we learned before that that means the next line will be indented by four spaces and now let's stick to a good practice so the good practice is to always document what the function does and how this is done is with a so called doc string and a doc string is simply a text object in python so a so called string object and because this is going to be written over several lines it is within triple quotes so we have three double quotes here and another three double quotes and in between I will break the line and now in between those triple double quotes we can write anything and this will just be the documentation for the function however there are a couple of conventions and the one that I like to use goes back to the so called google python style guide google as we know is a big company they use python a whole lot and so many people even outside the google universe adopted the google python style guide and we will do so in this course as well because it's a rather easy one to write and also to read so it goes like this the first line is the subject line which is a one line summary of what the function does so let's simply write calculate the average of all even numbers in a list dot period then we are going ahead and we leave one line empty and then we will write arcs colon and then we will simply mention the parameters or we will list all the parameters the same ones that are in the parameter list up here and we will briefly do the following we will first within parentheses specify what data type we expect so let's maybe right here we expect a list of ints of integers and these are just the whole numbers to be averaged so let's briefly describe that and then a function is going to return something and this is going to be called the return value and so we will simply write returns and for lack of better term or word we will simply call it average and then also in parentheses we will write the data type of what we will get back and this is a float data type so this is how you will see functions being defined you have the first line then you have a docks string and then in recent years it has become a best practice in the python world to use a syntactical concept called type annotations and this is something that is not so important for beginner but I will briefly show it to you how it looks like because this makes the function look a little bit nicer so instead of writing the data type in the docks string what people do is they will go ahead and write the data type followed by a column or separate by a column inside the first line and then we here have an arrow so it's the minus sign and an arrow to the right and we simply write float here and we could then go ahead and simply remove those parentheses and then the data types would be in the header line as well so these are two different ways of how to do it in this video I will or in this course also I will for now stick with this type to not make this a bit too complicated but just to already tell you that this is what you probably will see some people do some people will just write the data types here and that has a couple of advantages but for beginners it's not that important okay and now let's go inside the function definition and now we have to write the function so one way of how a beginner would probably do that in the beginning is we would simply go ahead and after we developed some code outside a function we would simply go ahead and maybe copy paste this code in here and of course every line has to be intended by four spaces and then in the last line in the function we have to specify what the function is going to return and there is a statement for that and it's called the return statement so we simply write return one space and then we are going to write one expression whose object to which it evaluates is given back by the function and in this case we will simply give back average okay so now first of all you may now wonder how does python not confuse the different variable name so we have outside the function we have variables even than average inside the function we also use them and in the next video when we talk about function scopes we will talk about when a variable is visible from where a variable is visible and when they disappear and so on for now let's just understand that python within the function will first of all look at the variables inside the function of course and this integers here refers to the parameter and now what we are going to do is we simply execute this code cell and we see that this code cell does not have a return value below it there is no output below the code cell and that means this code cell as a whole has a permanent side effect most likely and it does so the def statement creates a new function object in in memory so let's briefly go ahead and see what that looks like from the memory diagram from the previous video I already have the sum function in the memory diagram so in future videos I will not draw any functions that are built in because we can assume that they already exist as objects so this is just to contrast that to a built in to a user defined function and now what our function definition does is it basically creates a big box in memory it writes all the code in there it solves the problem then we also have a data type and the data type will be function so I will abbreviate it with func and then on the left hand side we will have a variable name and this is simply average events and this is simply going to reference the function object and that is what the memory looks like and that is also why we don't see anything below the code cell because the reference to the function is simply assigned that to a variable and whenever we do that simply we don't see anything below a code cell so now let's see what can we do with the function object well first of all we can of course go ahead and reference it so by just saying average events I get back a reference to the function just like I get back a reference to for example the sum built in function so this is basically referencing the function without executing it and now what we could do is we could go ahead and call the average events function and we do so of course with the call operator and if I now do that without input I get a type error and it says missing a required argument and that makes of course sense because we specified one parameter and we didn't provide the function call an argument for that so that is why what we are going to do here is we simply go ahead and we are going to give a reference to the list integers to the function by simply specifying or writing down the integers variable as the first argument to the function if I execute this I get back 7.0 and that is basically the return value and then often what you do is when you call a function let's copy paste this usually what you do with a function that gives something back you store the result under some variable let's call it result here and then by doing that we don't see any output below the code cell because the entire cell as a whole is not an expression it is a statement that is not an expression there is a previous video talking about the difference between what is an expression and what is not an expression however the result variable is created and now has the value 7.0 okay so let's end this video with a discussion of a couple of technical terms so first of all one important distinguishing is when we specify the input that the function takes we call that the parameters in this case we have one parameter when we call a function the input that we give a certain function called this is what we call an argument so an argument is what you put inside the function when you make it run and the parameter is the thing that you specify one in the moment when you specify the function okay so this is just two different words specifying either it is the view from the inside out so that is the parameter view or from the outside in what do we path inside the function and then we call that an argument so integers here would be the argument and of course what you could also do you could also go ahead and call the average even function and I create a new list object so to say on the fly so I could instead of passing the function a variable as the argument I could also pass the function a new object as the argument so let's go ahead and specify a list and let's simply write the numbers 40, 41, 42, 43 and 44 in there why do I choose these numbers well I want to make a sanity check here to see if the function is correct so I give the function input for which I can in my head calculate the output and averaging the evens in these five numbers I can clearly see that the average should be 42.0 so let's see if the function is correct and I get back 42.0 so this is avoiding semantic errors and there is also a previous video that talks about what semantic errors are and how they are different from two other kinds of errors called syntax errors and runtime errors so there is a whole lot more to be learned about functions we do that in separate videos one topic at a time but this is for now how you create a function you define it don't forget to always use a doc string sometimes what you could do is if you don't want to write everything down just write the one line summary that is often enough so you could actually remove the arcs and the returns part here so this is basically the basics of how to write a function so I see you in the next video