• No results found

Defining your own functions and modules

Using and making functions can make programming and debugging a lot easier. This is because it will make your main code shorter. It is also very useful when using the same calculation multiple times or to use the preprogrammed calculations.

6.1 Def statement: define a function

With Python you can define your own functions and in this way basically extend the language with your own commands. The statement def is used to define a function. The syntax of this statement is as follows: def functionname(arg1,arg2,arg3) : statement1 statement2 statement3 return resultvalue Examples: def fibo(n):

# Generate fibonacci serie until value n serie = [a,b]

while serie[-1]<=n:

x = serie[-2]+serie[-1] serie.append(x)

# remove last element which was larger than n

del s[-1]

return serie

Functions can be complete programs or simple one-liners. Another example with a very short function:

from math import sin

def f(x):

return x*sin(x)

Be aware that the def statement does not yet execute the function, it merely defines it. The code inside the function will only be executed when you call the function, after it has been defined

with def. So always call it, could be done manually from the interpreter to check the syntax and working of your newly defined function.

6.2 Multiple outputs

As you have seen in the formatting example, you can output several different values from your function. You can do this as a list, defined with the square brackets or a s tuple (a constant list ) with the round brackets or a tuple by leaving out the brackets.

When you call the function you can store the result in a list/tuple or in different variables. The latter is done in the example below, which explains how to output more than one value of a function.

6.3 Function with no outputs

When your function merely does something to the screen but there is no need to return a value, just use the return without a value. In the calling program, you can use this function just as you would use a statement.

6.4 Using one variable in definition as input and output is not possible

In some languages, a function can do something to the input variable without returning a value. This is not the case with the standard data types in Python, except for the list type.

In Python most data types are immutable, which means they are passed on as a value of the object is used in the function but since there is no reference to the memory location of the

original variable in the calling module will not be changed by anything the function does. See the example below. Most types of variables (floats, integers, strings, booleans) are immutable because they are passed on by value. Others (lists) are passed on as reference to a memory location, then tey would change with the function increase. These are called mutable variables.

Mind you, this use of one variable as input and output at the same time in the definition of a function is considered very bad programming practice anyway in the procedural programming style in the languages where this is possible. Yet at the same time, it is common practice in object oriented programming, also in Python! It would then be called a method of the variable a and called as: a.increaseit()

6.5 Using functions defined in other modules: managing a project

As you have already seen with the math functions in the previous chapter, they are defined in a separate file(module) math.py and to use them, you first need to import this module.

You can also use this principle of defining functions in different files for large programs where you want to keep an oversight by storing different sets of functions in different files, or as we say in Python: in different modules. In this case the module file needs to be in the same folder so Python can find it.

Suppose you have defined a function fibo in the file series.py , and now you want to use in your own program test.py . Then you need to import your module series. We usually do this at the beginning of the source, since you need to do this only once. Because once the function is defined, you can use it as many times as you want. There are now many ways in which you can import and call this function:

Method 1:

import series

s = series.fibo(10)

Method 2:

from series import fibo

s = fibo(10)

Method 3:

from series import fibo as fibonacci

s = fibonacci(10)

Method 4 which I would discourage: you do not know which other names now suddenly have a special meaning, it also makes it harder to trace where functions are defined (I only use this with math and sometimes with Numpy for the math functions:

from series import *

s = fibo(10)

On the internet you can find many 3rd party modules which extend the functionality of Python to do a range of things. Once they are installed it is hence very easy to use them: just add an import