Python Functions

Functions are a fundamental part of Python (and most other programming languages). Python includes a large number of built-in functions, but you can also create your own functions.

A function typically performs a specific task that you can run in your program. For example, print() is a built-in Python function that allows you to display text to the screen. Calling print("Hey") results in Hey being displayed.

One of the great things about functions is that they can be used like a "black box". You don't need to know how the function is coded in order to use it. In fact, you don't even need to see the function code before using it. All you need to know is how to call the function.

You call a function by referring to it's name, and providing any arguments that it might require. So calling print("Hey") calls the print() function and passes an argument of "Hey".

You can also create your own functions. This enables you to create code that can be reused over and over again. Code within the function doesn't actually run until you call the function.

Create a Function

Here's an example of a basic function.

This function simply multiplies a number by itself. The number is provided as an argument when the function is called.

Here's a rundown of each line:

  1. The def keyword introduces a function definition. It must be followed by the function name and the parenthesized list of formal parameters. In this case, the function name is multiplyMe and there's one parameter (x). This means that the user must pass one argument when calling the function.
  2. The next line is where the body of the function begins. The function's body must be indented. In our example, we simply multiply the argument by itself and assign it to a variable called y.
  3. The return statement is used to return a value from a function. In this case, we return the value of y.

So we can now call the above function like this:

Result
100

But the good thing here is, because the function doesn't actually print anything (it simply returns a value), we can also use it in other ways. For example, we can do stuff like this:

Result
325

This multiplies 10 by itself, then multiplies 15 by itself, then adds the two together. So we can perform other calculations on the returned value before using it.

Functions that Print Stuff

Of course, you can make a function print stuff if you need to. Simply use the print() function inside your function. Like this:

Result
49

Note two things about this:

  1. We dropped the return statement. We don't need to return the value because we're already printing it.
  2. When we call the function, we don't need to use print() anymore. This is because the function already does that.

Return Multiple Values

You can return multiple values from a function by separating them with a comma. This actually creates a tuple, which you can use as is, or unpack in order to use the individual values.

Here's an example of a function that returns multiple values:

Result
(33, 90, 0.1, -27)

You can tell by the parentheses that the result is returned as a tuple.

You can unpack each of those values by appending the function call with square brackets containing the value's index. Like this:

Result
33
90
0.1
-27

Create a Named Tuple

You can modify the above example to use a named tuple for the results. This can make it easier to reference each value within the tuple (see the bit where we print out each result). It can also make your code more readable and easier to debug.

Result
33
90
0.1
-27

Built-In Python Functions

Before you decide to sit down and write a wonderful function, ask yourself "is there already a build-in function for this?".

Python includes a number of functions built right into the interpreter that you can use straight away. Sometimes you might find that there's already a function that does what you want, or at least part of it.

For example, Python includes a sum() function that could be used instead of calculation.sum that we used in the above example. In fact, the built-in sum() function does more, as it allows you to provide a list of numbers (i.e. it can add more than two numbers at a time).

The following functions are always available, as they're built right in to the Python interpreter. If you're familiar with other programming languages, you'll probably recognize some of these and guess how they work. In any case, see the official docs for more information about how each one works.

You can also check the Python Package Index (PyPI), which is a massive repository of Python modules, packages, and even application frameworks).