This is an archived version of the course and is no longer updated. Please find the latest version of the course on the main webpage.

Introduction

In this module, we will look at functions (or more specifically functions in Python) in more detail.

We have mentioned functions through our module so far.

So what is a function?

Mathematically, a function is something that takes an input \(x\) and maps it to an output \(y\).

In programming, it is actually the same.

You have a function that takes a number of input arguments, and returns an output (or None).

In terms of our programming block, a function is yet another kind of block. Inside the block, you can assemble all our blocks in the usual way. What’s special about the function block is that inside the block, there may be variables that are not yet defined. So you cannot just run it as it is. You will have to call it, and you get teleported into an alternate universe that holds the block, and you also smuggle in some values from outside the universe to be used in the block. And then you can also take some values out from the block and bring it back to where you came from.

Using the definition from Think Python:

A function is a named sequence of statements that performs a computation. When you define a function, you specify the name and the sequence of statements. Later, you can “call” the function by name.