Why do functions only return one value?

Return value for functions

Functions are useful for using code that is used over and over again. So far we have always diligently entered data into the function for our functions in Python. In this chapter we get results from a function.

With the published results in the form of variables, we can then do more in the further program code as we wish.

Our functions then have the following structure:

In our example above, we pass the number 5 in our function with the meaningful name "". In the function, something is now done with the entered value - in the example simply doubled and then returned from the function via the result.

Outside of this, our function call is initially given a variable that is to accept the returned result and an equal sign.

Why pass variable over

Why do we even have to return the variable via the function? We are actually not returning the variable, but the value of the variable.

The variable is not available outside of the function. If we try in our Python program after calling the function directly to use the variable that is only used within the function outside of the function, we get the error message: "NameError: name 'return value' is not defined"

This is an extremely practical facility because we don't have to pay attention to the variable names used outside of the function when creating our function. We can use anything however we want.

Scope / scope of variables

Understanding the differences between global and local variables is extremely important when using variables inside and outside of functions.

Let's build a little Python program for understanding that is only there for the use of the variables. We have a function for this, and functions are integrated into this function. But step by step:

From now on, error messages are interesting. The program still runs smoothly for the first 2 lines. The variable with the name is set in front of the function and output directly. That works fine.

The function is then called and the variable set outside of the function is to be output immediately. Now we get our first error message "UnboundLocalError: local variable 'variableValue' referenced before assignment". Obviously, the variable does not exist within the function.

So we take this line out and reset the value of the variable within the function.

Now we no longer get an error message, but the same variable name is obviously different from the value - depending on whether it is used in or outside the function.

As a result we see:

Variable value before function: outside the function

Variable value in function: IN of the function

Variable value after function: outside the function

global variables

Now we increase the complexity, since we can also define a variable as.

We set up our variable "" within the function

Now we get the following output:

Variable value before function: outside the function

Variable value in function: IN of the function

Variable value after function: IN of the function

So we have overwritten the value of the variable set outside with a variable in the function.

And one more step!

nonlocal in Python for variables

Between and there is an intermediate form. To do this, you have to know that we can pack additional functions into functions, but these are only available for the respective function within the function. And here we can create a variable in the function in the function, which is then also available in the calling function.

This is something that should simply be reread if necessary. Here for the sake of completeness.

In the example it becomes clearer:

What do we get as a result?

1. Before function call: content set outside

2. in Fkt: content set within

3. Called up in Fkt - flt_local: content set within

4. Called in Fkt - flt_nonlocal: content within nonlocal

5. Called up in Fkt - flt_global: content within global

6. after function call: content within global

If you look at the result, the scope of the variables and the possibilities of extending them through and through becomes clear.

The key words are:

Advantages of functions

This means that our functions can also be reused for other projects! If we need this function again, we can transfer the function we have already created from an old project to our new project (regardless of which variable names were previously used in the function).

How we can easily integrate universally usable functions, we will see in the following chapter.

Recommend • Social Bookmarks • Thank you