Defining a custom function
1. Defining a custom function
Welcome back! Functions from Python's modules and packages are useful, but what if they still don't give us the functionality we need?2. Calculating the average
An example is calculating the average of a list, in our case, the list of order preparation times in minutes. We need to add up the values and then divide by the number of elements. To make the results easier to read, we can round the results to two decimal places.3. When to make a custom function
If we can't find a module or package that provides the function that we need, then we can make our own custom functions! A common guideline for when to use a custom function is the principle "Don't repeat yourself", usually abbreviated to DRY. This means that if we find ourselves writing the same lines of code multiple times, using complex syntax repeatedly, or performing the same task often, it's best to create a custom function instead.4. Creating a custom function
To make a function, we start with the def statement, which means "define".5. Creating a custom function
We then provide the name of our function. It can have any name, but we should make it descriptive yet concise, similar to how we think about naming variables.6. Creating a custom function
Next, we open parentheses.7. Creating a custom function
Inside, we provide the value or data structure that we want the function to work on, then close the parentheses. We can give this any name, but again, it should be descriptive. The item placed inside a function is called an argument, which is the information the function needs to do its job.8. Creating a custom function
We finish the line with a colon.9. Creating a custom function
The code inside the function is indented, and we can write as many lines as we like. We start by calculating the average of the variable provided to the function, values.10. Creating a custom function
Next, we round that calculation to two decimal places.11. Creating a custom function
Our task is complete, but we should note that any variables created in a function do not automatically exist outside of it. If we don't need any outputs, then we can finish our function there. However, as we need an output in this case, we can accomplish this by using the return keyword. This tells Python what we want to produce as an output.12. Creating a custom function
We finish by instructing Python to return our rounded_average variable. Later in the course, we'll cover best practices for documenting our functions.13. Using a custom function
Let's use the average function on our `preparation_times` list. We call our average function, inside which we pass our list. The output is as expected, but we can now complete this task in a single line of code, regardless of which variable we are working with! For example, to calculate the average daily orders, we simply call average again with the orders variable.14. Storing a function's output
While calling our function returns an output, we might want to store this information to use elsewhere in a program. As we've done with built-in functions, we can assign a variable equal to a call of a function, which will store the result of the function as the variable's value. Here, we create a variable called average_time that is equal to the call of average on the preparation_times variable. Printing the variable confirms the same value.15. Let's practice!
Time to build your own function!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.