When we use a function, we usually call the function , and then the contents of the called function will be executed in sequence. However, sometimes we need more than just the execution steps. We also need to obtain some variables in the function, so When we use a function, we can also add a return value to obtain some data in the function.
To return a value in Python, you need to use the return statement. Its syntax structure is:
returndata
Let's take an example to understand the return value. If we want to use a function to solve for the values of variable a and variable b, and then we want to output their sum in the main function, let's take a look at the code:
defget_sum(a,b):sum=a+breturnsumget_sum(1,2)
If we write the function in the above pattern and then output it, there will be no output result, because we return sum, which is equivalent to the value of this part of get_sum(1,2), but it is not operated on. If we modify Here's the code:
defget_sum(a,b):sum=a+bprint('This function was called')returnsumprint('Complete the transfer of the return value')s=get_sum(1,2)print(s)
The output is:
Called this function 3
Use this way to understand it. In the statement s = get_sum(1,2), the function is called first, and then the function is executed sequentially. After the return statement, the value of the function is equal to sum, and then the subsequent The statement will no longer be executed. After returning the value, s can perform an assignment operation, assign the return value of the function to s, and then output it to see our output result.
Let's take a look at the schematic:
When we use a function to return a value, sometimes it will not only return one value, but may also return multiple values. Let's take a look at how to return multiple values.
In fact, when we return multiple values, the principle is similar to when we return one value. One thing we need to pay attention to is that when we return multiple values, the multiple values are stored in tuples.
Let's look at the following example:
defget_data():a=1b=2c=3d=4returna,b,c,dprint('Return value type:',type(get_data()))print('Return value:',get_data())
Output result:
Return value type: <class'tuple'>Return value: (1,2,3,4)
We can see that when multiple values are returned, they are stored in a tuple and stored in a tuple. There are many ways we want to use this data.
We can then use the data returned above directly. We can use 4 variables to directly define,
i,o,k,l=get_data()print(i,o,k,l)
The output is:
1234
We can also print out the return value in a loop:
foriinget_data():print('This is the %dth data returned'%i)
The output is:
This is the 1st data returned This is the 2nd data returned This is the 3rd data returned This is the 4th data returned
That’s all for the return value. The return value is very important in the function structure. In this section, a simple example is used to introduce the return value of the function. Everyone should strengthen the use of the return value in subsequent studies.