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 that we need?2. Calculating the average
An example is calculating the average when working with a list. We need to add up the values and then divide by the number of elements. To make the results prettier 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! We should consider this approach if we would be required to repeatedly write many lines of code, use complex syntax, and would likely perform the task that this code would solve regularly. One common guidance is "Don't repeat yourself", usually abbreviated to DRY.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 be performed on, then close the parentheses. We can give this any name, so again we should be descriptive. The item we place inside a function is known as an argument and we'll learn more about this term later in the course.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.13. Returning a calculation
Alternatively, we can round the results within the return statement without first defining the rounded-average variable. Both approaches will produce the same results.14. Using a custom function
Let's use the average function on our sales variable. We call our average function, inside which we pass the sales 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!15. 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-sales that is equal to the call of average on the sales variable. Printing the variable confirms the same value.16. 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.