What is a function? – Using ”Functions” to Code
Share this post
What is a function?
A function is simply a “chunk” of code that you can use over and over again, rather than writing it out multiple times. Functions enable programmers to break down or decompose a problem into smaller chunks, each of which performs a particular task. Once a function is created, the details of how it works can almost be forgotten about. In this way the detail is abstracted, allowing the programmer to focus on the bigger picture.
(Some types of function are sometimes called procedures or subroutines. We’ll clarify this distinction later in this step, but for the rest of the course we’ll simply refer to them all as functions.)
Once a programmer defines a function, they can then call it whenever they need it, simply using its name. Additionally, in order to work, the function will probably require some inputs or parameters, which are given to the function each time it is called.
You’ll have used functions like this almost whenever you’ve written a program, as programming languages include built-in functions. For example, in Python:
The print
function, used to display messages on the screen, is called by giving the name of the function, print
, followed by a pair of parentheses (brackets) which contain the parameters you want to pass to it. In this case, the string "hello world"
is the only parameter. Functions can be thought of as little machines that perform a specific task, where the parameters are the inputs to the machine.
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 into 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")
Defining new functions
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"]
# 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 will 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.
- The
-
- 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.
- A colon
-
- 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()
Functions / Methods / Procedures
On occasion, you may encounter other terms to describe functions, such as procedure or subroutine. These refer to certain types of functions, depending on whether they have parameters or return values. We’re not going to use those terms during this course and instead will stick to using functions to describe them all. In places, however, you may notice that I 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 called a list (which we’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.
Over to you
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.
- Create a function that simulates a coin flip. Each time the function is called, it should randomly select either heads or tails and print it out.
Once you’ve completed the challenge, comment here and share a link to your code (e.g. using Pastebin), along with any comments or questions.
Share this post
Programming 102: Think Like a Computer Scientist

Programming 102: Think Like a Computer Scientist

Our purpose is to transform access to education.
We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.
We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.
Learn more about how FutureLearn is transforming access to education
Register to receive updates
-
Create an account to receive our newsletter, course recommendations and promotions.
Register for free