3.5. Functions

Function

Functions are named sequences of statements that execute some task.

We have already used functions, such as:

>>> type('GAATTC')
<type 'str'>
>>> len(cds)
852
      
For example len is a function that calculates the length of things and we asked here for the length of our DNA sequence cds.

Function call

Function calls are statements that execute or call a function. The Python syntax of function calls is the function name followed by a comma separated list of arguments inclosed into parentheses. Even if a function does not take any argument the parentheses are necessary.

Differences between function calls and variables.  As variable names, function names are stored in a namespace with a reference to their corresponding sequence of statements. When they are called, their name is searched in the namespace and the reference to their sequence of statements is returned. The procedure is the same as for variable names. But unlike them, the following parentheses indicate that the returned value is a sequence of statements that has to be executed. That's why they are even necessary for functions which are called without arguments.

Arguments of functions

Arguments are values provided to a function when the function is called. We will see more about them soon.