Skip main navigation

How do functions work in programming?

Functions make your code cleaner and save you a lot of time. Here, we take a look at how they work and gve you a few basic examples to run.

Functions are blocks of reusable and organised code that usually perform a single, related action. They are a crucial part of programming because they save you a lot of time and make your code cleaner. Here, we’ll take a look at how they work.

Defining your own functions

In Python, there are some ready-made, pre-defined functions that you can leverage. These are known as built-in functions. You are not just limited to the built-in functions though, you can also define your own.

If you define a function but do not call it, nothing will happen. Try it yourself.

Enter this code:

say_hi():
 print('Hello and welcome to my programme')
say_hi()

Now, remove the last line and rerun the code.

Styling

Note that Python advocates the use of snake case, which is lowercase with underscores between words (e.g. my_list) for functions and variable names; and PascalCase, which is capitalising the first letter of every word, (e.g. MyModel) for class names.

The features of this function example reflect most of the general principles of functions.

The return statement

Here is the code for the function you examined earlier:

def say_hi():
 print('Hello and welcome to my programme')
say_hi()

Output:

Hello and welcome to my programme

This example is somewhat incorrect. Usually, a function returns some kind of value. However, say_hi does not return a value but prints a string to the screen instead. While the programme does not give you any errors for doing this, it’s considered bad practice. Since the purpose of the example function is to output a string to the screen, you can change the code from a print() function to a return statement.

Try to modify and run the example function in your code editor now and see what happens:

def say_hi():
 return 'Hello and welcome to my program'
say_hi()

Output:

*empty*

You returned the welcome message, but the output is empty. What went wrong? The answer is that printing and returning are completely different actions. print() is a function that displays a given value to the console. return gives the final product of a function and therefore needs to be at the end of a function. Note that if you put it in the middle of your code block, all the function’s code after return will be ignored.

Do you see now that a function holds a returned value? If you want to use the returned value, you need to refer to the function that returns it. Try this code example:

def say_hi():
 return 'Hello and welcome to my programme'
print(say_hi())

Output:

Hello and welcome to my programme

In this final example, you used the print() function and inside it you passed the say_hi() function. Essentially, you printed the output of the function.

This article is from the free online

Introduction to Programming with Python

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now