What is a function in Python programming

Functions in Python

So far we have simply written our program code from top to bottom and it was processed in this order. Now it can happen that we need some program sequences more often. For example, we want to output the time at the start of the program and at the end of the program. To do this, we would have to duplicate the same code and write it at the beginning and at the end of our program. This inflates the program and creates unnecessary sources of error. Helpful functions here.

What is a function?

A function is a created program code that is taken from the "top-down" sequence of the program and must or can be specifically called. This enables us to call these functions more often if necessary and thus to write clear code that contains fewer sources of error.

So we give our function a name that we can call anywhere in our Python program.

A function is defined for this. And this is exactly the keyword that Python expects when specifying a function name. The typical brackets after that also show that it is a function. But let's define it as a function. Which does nothing else than the text output "Output of text from a function".

Everything that is indented belongs to our defined function.

If we now call our program, we only get the output:

The new function with the name itself was obviously not carried out, otherwise another text output would have had to take place.

To call a function we need the function name followed by the round brackets.

Now we get the expected output

Output text from a function

Program expired

We can call the function as often as we want. This is how we can recycle code and save typing:

Pass values ​​into the function

Parameters can be passed into the functions. To do this, we expand the brackets in the definition that were previously empty. Here we define the variable names. You can then work with these variables in the function. This value must be passed in brackets when the function is called. We pass the number 5 into the function:

The output is now:

Output text from a function

5

transfer several values ​​in function

If not just one value but several values ​​are to be transferred, the variable names are separated by commas.

Now you can of course work with the available variables as you wish. As an example, we now have 3 values ​​transferred into the function and then with the function an output (see previous chapter about) and the corresponding step size.

We now get the output:

4
6
8
Run through function output
5
6
7
Run through function output
have finished

So we have become much more flexible in terms of output. Depending on the application, the scope within a function is significantly more extensive than our 3 example lines. This does not change anything in terms of function and possible uses.

Standard setting when calling a function

In our last example, we issued a loop. In many cases the step size is very often 1. Therefore, it would be nice if we only have to specify the step size if it deviates from 1. And this is very easy to do with the Python function. We can set a default value. For this purpose, the variable is given the default value during definition via an equal sign. If no value comes from outside, the default value is used. If a value comes from outside the variable through the function call, it is used again.

As a result we get:

4
5
6
7
8
Run through function output
4
6
8
Run through function output

The first time it is called, the default value of 1 is used and we save typing in the standard case. With the second call, the transferred 2 is used as the increment.

If we now also want to set the start value for our output to 1, because this happens very often in our case, we can do that:

However, it leads to the error message "SyntaxError: non-default argument follows default argument". Why? The Python interpreter assigns the number 9 to our initial value and then expects an input for it in any case. Since this has no default, Python throws error messages at us.

The usual procedure here is that values ​​with specifications are simply to the right of values ​​without specifications. Let's rebuild our example accordingly:

Now our function also works with the call of just one entry. However, we have to keep in mind that the order in which the function is called is now first the

This is the usual practice and can save a lot of typing.

Recommend • Social Bookmarks • Thank you