Chapter 8. Defining Functions

Table of Contents

8.1. Defining Functions
8.2. Parameters and Arguments or the difference between a function definition and a function call
8.3. Functions and namespaces
8.4. Boolean functions

8.1. Defining Functions

In Section 3.5 we have learnt how to apply or call functions. So let's remember the example calculating the GC-percentage of a DNA sequence.

>>> float(count(cds, 'G') + count(cds, 'C')) / len(cds)
      
This calculates the gc percentage of the specific DNA sequence cds, but we can use the same formula to calculate the gc percentage of other DNA sequences. The only thing to do is to replace cds by the new DNA sequence in the formula. But it is not very convenient to remember the formula and retype it all the time. It would be much easier to type the following instead.
>>> gc('ATGCAT')
0.33333333333333331
>>> gc(cds)
0.54460093896713613
      
The only thing we have to remember is the name of the new function and its use.

Abstraction

The possibility to define such new function executing tasks specified by yourself, is an abstraction feature, provided by all high level programming languages.

Important

It is also important to notice that functions have to be defined before they are called. You can not use something that is not defined.

Here is the syntax of such a new definition in Python:

>>> from string import *

>>> def gc(seq):
...     return float(count(seq, 'G') + count(seq, 'C')) / len(seq)
      

Let's have a closer look at this definition. Figure 8.1 illustrates the structure of a function definition.

Figure 8.1. Function definitions

def and return are basic instructions.

Basic instruction

Basic instructions are statements that define the language rules and the semantic of Python. They constitute the basic set of instructions of Python. Each basic instruction has its own syntax that you have to learn in order to master the programming language.

The return basic instruction is used to return the result of a function back, in our example the value of the GC percentage of the specified DNA sequence.

The def basic instruction indicates to Python that a function definition follows. It has to be followed by the new function name and a comma separated list of parameter names enclosed into parentheses.

Parameter

Parameters are variable names. When the function is called they are bound in the same order to the arguments given.

The body of a function contains the piece of code needed to execute the subtask of the function. In the example above, the body contains only the return statement. Here is a more complex example that excludes ambiguous bases from the GC percentage calculation.

>>> from string import *

>>> def gc(seq):
...    nbases = count(seq, 'N')
...    gcpercent = float(count(seq, 'G') + count(seq, 'C')) / (len(seq) - nbases)
...    return gcpercent
      
In this example the body of the function contains three instructions (2 assignments and the return statement). The body of a function follows the definition line and is written as an indented block initiated by a colon.

Block

Blocks are structure elements of a program, that are used to group instructions. They are initiated by a colon and are separated from the rest of the code by the same indentation step in Python.

In our example, the function body is written as a block to separate it from the rest of the program. The Python interpreter used it to detect where the function body starts and ends.

Important

In other programming languages special words or characters are used to indicate the begin and the end of blocks and the indentation is only used to make the code readable for humans. In Python the indentation is used explicitly to do this. So in the above example of a function definition the end of the body is detected when the next line starts without any indentation step. This is also illustrated in Figure 8.2.

Example 8.1. More complex function definition

>>> from string import *

>>> def gc(seq):
...    nbases = count(seq, 'N')
...    gcpercent = float(count(seq, 'G') + count(seq, 'C')) /  \
                   (len(seq) - nbases)
...    return gcpercent
...
>>> gc('ATGCN')
0.5
	

Figure 8.2. Blocks and indentation