Functions

General informations
 are some bunch of code. The advantage (and reason for creating them) of functions is the fact, that we can use them (and with this the specified code) everywhere in the program and in other Classes.

Syntax of functions
To define a function, we use the keyword def. After this we write the function name and a : All code that belongs to this function now has to be indented, as Python does not feature braces.

A basic example (version 3.X):

Parameters
The mechanism of parameter-passing in Python is a special theme. But this won't be discussed here, we just look at how we can pass them to functions:

Note that there can be multiple parameters and also two special things discussed below.

*args
Sometimes it's useful that a function can handle a variable number of parameters. If your are familiar with C or C++, this is equivalent to the *varargs thing.

To make this available, we have to assing a paramter with an Asterisk, the common wildcard standing for anything:

**kwargs
Sometimes it's also useful to make a function handle several variable keyword arguments, an example:

A further advantage is that we can define how to use several parameters, but the function will also do the right thing if we pass this variable keyword parameter not.

The best explanation is the following example, taken from the Python tutorial:

Note: For the example above the voltage-parameter has to be passed everytime. So the following woule be invalid: