Chapter 2. Defining Python Functions
In Chapter 1 - What is Programming? you learned about the goals of programming and how to break down problems to put them into functions. In this chapter you will learn how to write functions in a programming language called Python. There are many programming languages just like there are many human languages. Python happens to be one of the easiest to get started with quickly.
By the end of the chapter you should be able to:
- list the elements in a Python function
- state what the elements of a Python function represent
- list the elements of a Python function that are optional
- write code to define a Python function
- state three best practices for defining Python functions
The Elements of a Python Function
There are eight elements to a Python function. These elements provide a structure for the code to run on the computer. Not all of them are needed in every function. Function 2.1 gives an example of a Python function that we will use to explain the elements of a function. Table 2.1 lists and defines the elements.
def add_two_numbers (summand_1, summand_2):
sum = su_mmand_1 + summand_2
return sum
The purpose of Function 2.1 is to add two numbers together. While this is not a very complex function, it is a great starting point for beginners. It is perfectly normal and expected that your eyes just glaze over when you look at Function 2.1. Very few people just starting to program will understand all of the components. Take a look at Table 2.1 and compare the elements to Function 2.1.
| Element | Required? | Explanation |
|---|---|---|
| def | Yes | The def comes at the very front of the definition of a function and lets the computer know that you are about to define a function. |
| function name | Yes | The function name comes after def and gives the computer a unique way to find your function. This is typically represented with underscores between words. |
| ( ) | Yes | The parentheses give your function a place to be given inputs. Not all functions have inputs, but all functions must have the parentheses. |
| inputs | No | The inputs are comma separated variables to be used in the function. You do not need inputs for every function. In Function 2.1 summand_1 and summand_2 are inputs. |
| : | Yes | The colon goes after the closing parenthesis. It signifies that the process code is coming next. |
| indentation | Yes | Everything after the colon has to be indented to represent that the process code belongs the function. |
| process code | Yes | The process code is all of the code that the function needs to perform each time it is called. |
| return | No | The return specifies a variable to output to whatever called the function. Not every function will have an output. |
Defining a new Python function
Now that you know the elements of a Python function, you need to practice writing one of your own. This section will walk you through writing the first line of a function. We need a function to figure out the percentage of the month that is remaining. Right now, we are just going to write the first line. In the next chapter we will finish the function. The first word we need is def.
def
Second, we need to give the function a name. We could call the function my_function, however, it is easier to understand what the function is doing if we give it a descriptive name. We want this function to calculate the percentage of the remainder of the month, so, to be descriptive we can call the function:
calculate_percentage_of_month_remaining
Third, we need to add the parentheses and the inputs. We need the current day in the month and the number of days in the month to make this function work. We will use the variables current_day_of_month and total_days_in_month. Due to the limitations of the width of your screen, all of those words may not be visible. Therefore, you may have to scroll the code to see the whole line.
def calculage_percentage_of_month_remaining (current_day_of_month, total_days_in_month)
Lastly, we need to end the definition with a colon.
def calculage_percentage_of_month_remaining (current_day_of_month, total_days_in_month):
Best Practices for Defining Functions
There are many ways to define a function. However, there are some practices that are better for later readability and for reducing errors in the programming (typically called bugs). We discussed two already and we will discuss two more. Here are four best practices for defining functions:
- Function names should use underscores between words
- Function names should be descriptive
- Functions should have a single purpose
- Variables in functions should be renamed once something is changed
Using underscores between words in function names (and variables) allows functions to be read quickly and distinguished easily from other programming elements. Using descriptive function names allows you to quickly recognize the purpose of the function. The third best practice is that each function should have a single purpose. Functions with a single purpose are easier to debug, reuse, and understand. If you have an ‘and’ in the function name, it is likely doing more than one thing and should be split into two or more functions.
The last best practice is that variables should be renamed after they change. In Function 2.2 we want to convert inches to miles, but we first want to go through feet. Therefore, we convert inches to feet and then feet to miles.
def convert_inches_to_miles (inches):
new_length = inches / 12
new_length = new_length / 5280
return new_length
In Function 2.2 the variable new_length has two problems, first, it is not descriptive of what it stores and second, the variable value changes without alerting the programmer. These problems can cause bugs and make the function harder to interpret in when another programmer reads the function. Function 2.3 shows a better version.
def convert_inches_to_miles (inches):
feet = inches / 12
miles = feet / 5280
return miles
Practice
- parentheses
- values
- semi-colons
- a def
() : def add_two_numbers
- input
- output
- process
- function name
- def
- ()
- :
- return
subtract_two_numbers (minuend, subtrahend)
Answers to practice problems
- C
- def add_two_numbers ():
- A
- D
- def and :
- easier to debug, reuse, and understand