Functions
Functions
A function is a way to dramatically improve the design of your code. A function has some input, it does something with it, and then produces some output. You’ve already been using functions in python. For example the function len()
:
some_list = ['so', 'long', 'and', 'thanks', 'for', 'all', 'the', 'fish']
x = len(some_list)
print(x)
The function len()
, takes a list as input, then it counts the element, and as an output it gives the length of the list:
- The input
some_list
is given between the parentheses:len(some_list)
- The output of the function is assigned to
x
by the equals (=
) operator:x = len(some_list)
The function len()
is built into Python, so we don’t have to worry about how it works internally, as long as we know how to use it. Let’s, however, pretend that such a function does not exist, how would we define len
ourselves? Let’s call this function length
:
def length(lst):
count = 0
for e in lst:
count += 1
return count
some_list = ['so', 'long', 'and', 'thanks', 'for', 'all', 'the', 'fish']
x = length(some_list)
print(x)
The function length
does the exact same as len
. It takes a list as input, counts the elements, and returns the length of the list. A couple of things to notice:
- The start of a function is denoted by the word
def
, which is followed by the function name, which you can choose for yourself. We chose the namelength
. - Then the parentheses
()
which may or may not contain any parameters. - If we have parameters, we write them inside the parentheses. In this case we define the parameter
lst
. - Don’t forget the colon
:
at the end of the line. - On the next lines we have the body of the function. Here we define the variable
count
and count the elements of the list. - The command
return
is used to specify which value should be returned by the function. In this case we want to return the value of the variablecount
, so we writereturn count
.
Keep in mind that the function stops running as soon as return
is encountered, even if there is still code after the return
.
Functions with multiple parameters
A function can have more than one parameter. For example, if I want a function that computes the value (), I can define a function like this:
def my_fun(a, b):
y = a**2 + b
return y
my_val = my_fun(3, 2)
print(my_val)
We’ve defined a function similar to the way we did above. The main novelty here is that the function my_fun
accepts two parameters, a
and b
.
Note that the order of the values 3 and 2, provided when we call the function (my_val = my_fun(3, 2)
), should correspond to the order of the input parameters in the definition of the function (def my_fun(a, b):
). The call my_val = my_fun(2, 3)
would give a different output.
Details
Read more about functions here: Think Python
Style and design
Using functions improves the readability of your code. With a well-chosen name for each function you can easily improve the readability of your code. If a program is well designed you can often get an overview of what it does by simply looking at the function names.
Functions can also be expedient when you repeatedly have to use a more or less identical piece of code. Oftentimes when you catch yourself copy-pasting parts of your own code, you’d better spend this time trying to define a function!