Python: An introduction to functions, part 1

A function is a named block of code that is used to perform computations. Python provides many in-built functions such as print()
and zip()
, but functions can also be created by users.
Why create functions? Functions provide a way to package code so it is easy to reuse when the same action needs to be performed repeatedly. This means there is only one copy of code in one place at any given time, and data can be analysed more efficiently in a way that is reproducible.
How functions operate takes a while to get used to, so we shall start with some simple functions to see how they work. Let’s start with the classic example of defining a function to convert temperatures from Fahrenheit to Kelvin:
def fahr_to_kelvin(temp):
"""Convert temperature in Fahrenheit to Kelvin """
return ((temp - 32) * (5/9)) + 273.15
Some points on writing functions in Python:
- A function block begins with
def
, then the function name, parentheses()
and a colon:
- Variables to be passed to the function as inputs are placed in the parentheses
- Any code belonging to the function is indented by 4 spaces
- The first line(s) after the beginning of a function block enclosed by triple quotes
""" """
is the documentation string (docstring) of the function. Information on what the function computes is written here - The
return
statement is used to pass information from the function as outputs back to the bit of code that called the function. (Not all functions needreturn
statements or input variables in parentheses)
Python interprets the code above as: the function fahr_to_kelvin()
is defined to take a value (known as temp
in the function), perform a mathematical computation on that value, and return the new value. We can use the print()
function to call the temperature function:
print('freezing point of water in Kelvin:', fahr_to_kelvin(32))
which produces the following output:
freezing point of water in Kelvin: 273.15
Python interprets the print statement this way: first, the text freezing point of water in Kelvin
is printed, then Python calls the function fahr_to_kelvin()
defined earlier, inputs the value 32
to the function, performs the computation and returns the new value 273.15
back so it is printed.
Since we have converted temperature from Fahrenheit to Kelvin, we can also convert temperature from Kelvin to Celsius:
def kelvin_to_celsius(temp_k):
"""Convert temperature in Kelvin to Celsius"""
return temp_k - 273.15
and combine both these functions in a single function to convert Fahrenheit to Celsius directly:
def fahr_to_celsius(temp_f):
"""Convert temperature in Fahrenheit to Celsius"""
temp_k = fahr_to_kelvin(temp_f)
temp_c = kelvin_to_celsius(temp_k)
return temp_c
print('freezing point of water in Celsius:', fahr_to_celsius(32.0))
which prints:
freezing point of water in Celsius: 0.0
Summary
We wrote some simple functions to compute temperature values from one scale to another, and wrote a function to call other functions. In the next step we will expand on the idea that a function only works with variables provided as input values, or variables created inside the function.