Functions

So far in the course you have used function to code your solutions. Functions are one of the main components of programs, and a big computer program is usually a collection of many many functions that interact between each other. A good function is one that:

For example, in several exercises in this course you needed to find out if a number is prime or not. Instead of copying and pasting that piece of code from one place to the other, the correct thing to do is to create a function, and call this function at the places you need.

Many times solving a problem involves splitting it into smaller parts, solving the parts, and combining these into a solution for the bigger problem. It might help to implement a function for each of the smaller parts. The advantages of this approach are:

Finding how to organize your code into a set of functions is a little bit like art. You need to be able to find beauty and function in order.

Function lexicon

We have used a lot of the words related to functions during the course. Let's give them a precise meaning now.

More about local and global variables

So far we have only dealt with local variables. Since our programs consisted of only one function, there was no need to have global variables shared by all functions. If many functions need to access the same information, and this cannot be easily passed as a parameter, it is sometimes useful to keep this information in a global variable.

For the scope of this course, you will never be required to use global variables, but you can if you think this will make life easier. Be careful though... It is very easy to lose track of when and how global variables were modified, and debugging might become a nightmare.

The code below shows how global variables work.

Interesting facts about functions

Docstring

When defining functions, you can provide them with a help text placed between triple quotes below the first line of the function declaration. This help text, called docstring can be accessed by help(functionName). You can try to call help on function that you already know to see their docstring.

Default parameters

It is possible to define functions with default parameters. These are optional parameters that take a default value in case the user does not pass a value in its place.

Named parameters

Functions can have named parameters, and this way the order of parameters can be changed, or some of them can be ommitted (taking their default value). Many functions in the python libraries have named parameters. For example, when you use sorted(L, reverse=True), you are using the named paramter reverse from the sorted function.

Example: Sudoku

As an example on how we can use functions to avoid code repetition, let's develop a program that checks if a sudoku solution is valid. Sudoku is a game on a 9x9 board that needs to be completed by filling each position with one of the numbers 1 through 9. Initially the board has already some numbers on it. A solved board is one where each column, each row, and each of the nine 3×3 tiles that compose the board contain all of the digits from 1 to 9.

Since we need to check some conditions for several parts of the board, we will make functions for checking that separate from our main function.

Example: image processing

Images are represented in a computer as a matrix of pixels. Each pixel contains information of what color it should be, typically a triple (r,g,b) indicating the amount of red, green, and blue mixed to form the pixel's color. These values vary from 0 to 255, with 0 indicating there is no amount of that color component (i.e. black), and 255 indicating that the color component occurs with maximum intensity.

Image filters work by changing the values of these pixels. The transformation of each pixel can be implemented using a function, and this function can be passed as an argument to a function just like a normal variable. See an example in the code below.