Some functions, including print, can be called without any parameters. However every function call must include the parentheses used to pass parameters, even if they are empty.
So whilst the line below is a perfectly valid function call:
print()
this next line wouldn’t call the print function, since without the parentheses, Python would assume print was the name of a variable:
print
Importing additional functions
There are lots of functions built in to Python that are available straight away. Others, however, you will need to import before you can use them. An example of such a function is sleep, part of the time library, which enables your program to pause for a period of time. To be able to use this function in your program, you would import it at the beginning, as in this example.
from time import sleep #imports the sleep function from the time library
print("Waiting 5 seconds")
sleep(5)
print("Done")
Creating a new function is fairly straightforward and is something you should consider doing for any task you’re likely to want your program to do regularly.
For example, imagine you have a program in which you will often need to print out the contents of a list line by line. The code to do this might look like this:
#Create an initial shopping list
shopping_list = ["Bread", "Milk", "Apples", "Chocolate", "Paprika", "Basmati Rice"]
# Print each item in the list line by line
for item in shopping_list:
print(item)
This is only three lines of code, but if you need to print lists regularly, you should create a function. To create a new function, you need to define it before it is used for the first time, usually at the start of your program.
#Define a new function display_list
def display_list():
for item in shopping_list:
print(item)
The definition is structured as follows:
The def keyword tells Python to expect a definition.
A simple yet descriptive name for the function.
A comma-separated list of the parameters, surrounded by parentheses.
A colon : to signify an indented block of code follows.
An indented block of code, containing the body of the function.
Once defined, this function can be simply called, using its name and a pair of parentheses (empty in this case, since the function takes no parameters).
display_list()
# Add item to the list
shopping_list.append("Sugar")
#Display updated list
display_list()
Subprogram terminology (Subroutines / Functions / Methods / Procedures)
On occasion, you may encounter other terms to describe subprograms (small “chunks” of reusable code) such as procedure or subroutine. These refer to certain types of subprograms, depending on whether they have parameters or return values. I’m not going to use those terms during this course, instead I will stick to using functions to describe them all. However, you may notice that I sometimes use the term method in place of function. There is little difference between these terms. A method is simply a function that belongs to or is part of another structure called a class. It works in the same way, but is called slightly differently.
In the example above, I used the append method:
shopping_list.append("Sugar")
shopping_list is a type of data structure call a list (which you’ll look at next week). All lists have the associated function append: this function belongs to the list, and is therefore known as one of its methods. You can find out more about objects, classes, and methods, by taking part in our Object-oriented programming course.
Can you create your own simple functions? Pick one of these challenges to complete, and share in the comments for this step:
Create a haiku function, which prints out each line of a haiku one by one.
Create a function that asks the user for the height and base of a triangle, and then calculates and prints out its area. (Note: The formula for the area of a triangle is base*height/2)
Create function that simulates a coin flip. Each time the function is called, it should randomly select either heads or tails and print it out. (You will need the choice function from the random library to do this. choice will randomly select an item from a given list. If you are not sure about importing modules, choose one of the above tasks to do instead.)