27  Functions

Often, when we write things we want a computer to do, we want to do them more than once, maybe at different points in our computer program. Or even in a different program altogether. We want stuff to be reusable without having to write it out each time.

To prevent us having to write out the same sets of instructions time and again, we can wrap a set of instructions up in a function.

A function can take inputs (optionally), do something with them, and then return an output (optionally). The things it does are just sets of instructions.

A function is defined by giving it a name, the list of inputs it should expect (if any), the things we want it to do, and the outputs it should return (if any). The function doesn’t do anything until it is called, however.

27.1 Writing functions

As well as the many in-built functions and those in external libraries, we can also write our own functions. Indeed, most programs will (and should) have their own functions, so we don’t have to repeat the same code multiple times.

To define a function we use the def command. We specify the function name, the input(s) to the function (if any), and the block of code representing the function’s inner workings. We also usually need to return an output (though not always).

Tip

Note : Good function design is to have the function to do one thing well.

Let’s consider an example. Let’s say we want to write a function that takes two numbers, multiplies them together, works out whether the result is higher than 100 and returns a Boolean to indicate this.

27.2 Calling a function

Tip

We’ve now written a function, but at the moment we’ve just told Python that we want a function that looks like this.

Nothing will happen until we tell Python we want to use the function - this is known as calling the function.

Try running the cell below. Do you see any output?

Now let’s try again

We can call the function anywhere in our code after the function has been defined, and we can call it as many times as we like.

As long as you’ve run one of the cells above, the cells below should work as Python has remembered our function.

We must provide any required inputs when we call a function.

If an output is returned, we must store it somewhere. In the function below, instead of printing, we are returning a boolean value (true or false)

We could also just print the result directly by wrapping our call to our new function inside the print function.

Note - if we pass in variable names as inputs to a function, they don’t need to match the names in the function definition.

27.3 Functions without inputs

Some functions don’t need inputs and / or outputs. Here’s an example of one that has neither inputs nor outputs.

In the above example, the function definition contains nothing in the brackets (no inputs) and no return statements (no outputs - nothing passed back). Because there are no outputs, we just call the function without assigning the output to a variable (because there isn’t one).

27.4 Returning multiple values

Sometimes, you may want to return multiple values from a function. There are basically two ways to do this : Return the values separately Return the values stored in a single structure (eg a list)

For the first approach, we list out the values being returned using commas in the return statement, and list out the variables to store those values using commas in the variable assignment when calling the function.

Let’s look at an example.

Let’s see how we could do the same but returning as a list.

27.5 Global and local variables

When using functions in Python, it’s important to understand the difference between a local and a global variable.

A local variable is one which lives inside a function, but it has no visibility outside of that. In other words, nothing outside the function can see or use that variable.

In the previous example, low, high, number_1, number_2 and number_3 are local variables. If we tried to reference them outside of the function, we’d get an error (though we can create new variables with the same names, but they are different variables to the ones in the function).

A global variable is one which is defined outside of a function. These can be used and referenced anywhere - all of the Python code can see it. However, if we want to change them inside a function, we have to use something known as the global keyword.

Warning

This is not typically recommended though, and is considered bad practice (and can lead to problems, as you can likely imagine), so we won’t teach it here.