Chapter 2. Defining Python Functions

Version 1.0 - 27 June 2016

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:

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
Function 2.1 Adding two numbers

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.
Table 2.1 The elements of a function

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:

  1. Function names should use underscores between words
  2. Function names should be descriptive
  3. Functions should have a single purpose
  4. 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
Function 2.2 Converting inches to miles (bad)

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
Function 2.3 Converting inches to miles (better)

Practice

1. A Python function does NOT contain which of the following?
  1. parentheses
  2. values
  3. semi-colons
  4. a def
2. Arrange these elements of a Python function in order:
() : def add_two_numbers
3. Which of the following does the ( ) represent?
  1. input
  2. output
  3. process
  4. function name
4. Which of the following is not needed in every function?
  1. def
  2. ()
  3. :
  4. return
5. What is missing in the following function definition?
subtract_two_numbers (minuend, subtrahend)
6. List the reasons why functions should have a single purpose.

Answers to practice problems

  1. C
  2. def add_two_numbers ():
  3. A
  4. D
  5. def and :
  6. easier to debug, reuse, and understand
Chapter 3. Evaluating Python Functions →