Exercise 5 Functions Modules 2021
Exercise 5 Functions Modules 2021
The purpose of this exercise is to learn how to write functions and modules in Python. You
will also get an introduction to local and global variables. Also read about functions in
Chapter 3 and first part or Chapter 6 in the course book.
Functions
In Python the keyword def is used to indicate the start of a function. In the example below
def tells that this is a function. The name of the function is 'hello_world'. Note the colon (:)
last in the function definition. Then the body of the function is indented in the same way as
e.g. a for-loop.
def hello_world():
print('Hello World!')
hello_world()
If you want to pass argument(s) to a function you add them within the parenthesis, and if
you want the function to return something you use the keyword return. The function below
takes two arguments, the height h and base b of a rectangle and returns the area. It is
possible to make the function shorter by omitting the variable area and directly return h*b,
but making the function more compact will also make it more difficult to understand. You
should also add a docstring to the function that explains what the function does.
area = h * b
return area
Often functions (or methods) are written as part of a larger scripts and often the same script
contains several functions (or methods). The code below in an example of a script that
contains three functions and one function call for each of the functions. Note that the
functions must be given first in the script. When the Python interpreter reads the code it
creates one function object for each function. When calling a function this function object is
called. It you try to call a function before the function object is created you will get an error
message. Also try to call a function without the parenthesis. If you e.g. do:
That means you are printing the function object, not calling the function.
###############################################################
def hello_World():
# This function only prints 'Hello World!'
print('Hello World!')
def print_2_times(inStr):
# This function prints the given string twice.
print(inStr + ' ' + inStr)
def print_multiple(in_str,nbr_print):
# This function prints the given string nbr_print times
print((in_str + ' ')*nbr_print)
hello_World()
print_2_times('Python')
print_multiple('Hello', 3)
###############################################################
When you create a variable in a function, that variable only exists locally in that specific
function. If you have another variable with the same name outside the function the two
variables will be different with the one outside the function treated as a global variable and
the one inside the function treated as a local variable as in Figure 1. Note that global refers
to within the current script, not to other scripts and files. Here the global variable x is
assigned the value 42. When fun_1() is called a local variable x is created. When fun_1()
stops the local x = 1, will be deleted and the global x still has the value 42. When fun_2() is
called the keyword global tells the interpreter to point at the global variable x. After fun_2()
has been called the global variable x has the value 1.
Figure 1. Global and local variables.
###############################################################
x = 42
def fun_1():
x=1
def fun_2():
global x
x=1
fun_1()
print(f'The value of x is: {x}')
>>>> The value of x is: 42
fun_2()
print(f'The value of x is: {x}')
>>>> The value of x is: 1
###############################################################
Modules
A module is like a collection of function(s) in a script that you can import and call from
another script. Before you can use the functions in the module you need to import it as with
e.g. the math module. Let’s go back to the rectangle_area() function. Write this in a file
called area.py and save it.
#######################################
def rectangle_area(h, b):
'''
This function returns the area of a rectangle
Arguments: h = height and b = base
'''
area = h * b
return area
#######################################
Now you can import this module from any other script. Create a new script and add the code
below:
import area
print(area.rectangle_area(2,3))
Note that you need to use the dot-notation area.rectangle_area(). You can also add more
function, e.g. circle_area(), triangle_area() to your area.py module.
if __name__ == "__main__":
Before you have created functions and added function calls in the same script. If you create
a module you can do it in a similar fashion and have lines of code to test the module. But if
you import the module you only want access to the functions in the module, you don’t want
the test code to run. One solution to this is to add the line of code below before the test
code in the module (it will be described more in detail later):
if __name__ == "__main__":
__name__ is a special variable in Python. If you run a script the value of __name__ will be
__main__ and your test code will run. If you import the script as a module the value of
__name__ will be the actual name of the imported module.
###############################################
def import_test(in_str):
print(in_str + ' ' * 2)
>>>import import_test
>>>import_test.import_test('Printing from test script')
The output will be:
Which means the test code did run when importing the module (the first line), and then
Python printed the test code after importing the module (second line). This is (probably) not
what you want. If you add if __name__ == "__main__": before the test code in the module:
###############################################
def import_test(in_str):
print(in_str + ' ' * 2)
if __name__ == "__main__":
# Code below is only for testing the function
import_test('Printing from function import_test')
###############################################
The module will not print the test code when it is imported as a module. Another option is to
perform all the tests in another script but it is handy to keep tests in the actual module when
you develop it. Another common version is to add def main(): before the test code and test
for __main__ last as below:
def main():
#test code comes in this block of code.
if __name__ == "__main__":
main()
Then the test code will not run if you import the code as a module. It is also possible to use
the __main__ test in all scripts. You could e.g. add it to all the scripts you have created and
they would run as before.
import math
When you do so a module object is created. Try print(math) and you will get a message like:
<module 'math' (built-in)> If you do: print(math.sin) you will get a message like: <built-in
function sin> That means that when you import the module, Python creates function objects
for all functions in the module, but it does not execute the functions.
which means you import the math module and gives it the alias m. This way you call the
square root function as: m.sqrt(9)
An advantage with both of the methods above is that you know which sqrt() function you
are calling. You can also import a specific function: from math import sqrt()
to make sure you know which pi you are using. You can also import all (*) functions and
constants from math: from math import *
Note that the constants e (e) and π (pi) are added to the variable explorer since these are
part of the math module. After importing all from math you can simply write: sqrt(9)
A good practice is to import modules in a way that you are sure which function or constant
you are using to avoid mistakes. Write the lines of code below in the Python console to see
an example of a situation you want to avoid:
#######################################
>>> from math import *
>>> pi
3.141592653589793
>>> pi = 32
>>> pi
32
#######################################
Write a module area with functions for computing the area of (1) a rectangle, (2) a circle and
(3) a triangle (area = h*b/2). Note that you can call other function from within the module.
The area of a triangle is e.g. half the area of a rectangle with the same height and base. Also
write a script to test your area module. A possible structure of the module could be as
below. Note that if you start by adding function definitions like this you need to have more
than comments inside the functions otherwise you will get an error message. To avoid this
error message you can add pass as below. When you start adding code to the function
remove pass. Remember the indentations!
Also note:
if __name__ == "__main__":
#######################################
def rectangle_area(h, b):
# This function returns the area of a rectangle
# Arguments: h = height and b = base
def circle_area(r):
# This function returns the area of a circle
# Arguments: r = radius
if __name__ == "__main__":
pass # Remove pass when you start adding code.
# Here you can add code to test the functions while you develop the module.
# This code will only be executed when you run the script.
# This code will not be executed when you import the script as a module
#######################################
A possible structure of a script to import and test your module could be as below. While
developing the module it is often more convenient to add test code after if __name__ ==
"__main__": as above instead of working with both test script and module at the same time.
#######################################
import area
print(area.rectangle_area(h, b))
print(area.circle_area(r))
print(area.triangle_area(h, b))
#######################################
Task 2:
In this task you will write a function that computes the area of a polygon. The area of a
polygon (Figure 1) can be computed according to Equation 1. The argument to the function
is a list with the coordinates of the vertices as (x, y)-tuples: poly = [(3, 9), (8, 9), (9, 4), (4, 2),
(2, 3)]. This means that you need to write a loop that iterates over all the vertices in the
polygon. Since you have xi+1 and xi-1 you also need to handle the special cases at the first and
the last vertices. The function shall return the area and be well documented. You can write
one script containing both the function and the function call to make it simpler.
Figure 1. Polygon.
𝟏
𝒂𝒓𝒆𝒂 = ∑𝒏𝒊=𝟏 𝒚𝒊 (𝒙𝒊+𝟏 − 𝒙𝒊−𝟏 ) (Equation 1)
𝟐