This section describes how Python functions return multiple values at the same time.
When learning Python, I was surprised to find that Python functions can return multiple values at the same time, which is very interesting.
#define function sumdef sum(x, y): z = x + y x = x ^ y y = x ^ y x = x ^ y return z, x, y#define function maindef main(): a = int(raw_input("plz input a ")) b = int(raw_input("plz input b ")) c = sum(a, b) print(c)#entry of programmemain()
First, let's take a look at the execution results of the program.
plz input a 12plz input b 13(25, 13, 12)
In the result (, 12), it looks strange. This is the tuple (tuples) in Python. It's a new term. It doesn't matter. We will mention it later in list. In the sub-function sum, in addition to the sum (3rd rows), there are operation operations for exchanging the values of x and y (4th ~ 6 rows), returns the print results (7th rows) of z, x, and y (13th rows) to the c (14th rows) of the main function. The x and y values are indeed exchanged.
Does a and B in the main function change? A, 12, B, or 13? In the main function, we add two print statements after print (c) (14th rows) to print the values of a and B (15th and 16 rows), respectively ), check if a and B have changed?
#define function sumdef sum(x, y): z = x + y x = x ^ y y = x ^ y x = x ^ y return z, x, y#define function maindef main(): a = int(raw_input("plz input a ")) b = int(raw_input("plz input b ")) c = sum(a, b) print(c) print(a) print(b)#entry of programmemain()
Running result
plz input a 12plz input b 13(25, 13, 12)1213
The conclusion is that the values of a and B are not affected.
When the sum function is called, the values of a and B are transmitted to x and y (both a and x point to the data unit of 12) (both B and y point to the data unit where 13 is located.) In the sum function, x and y change the data unit that x and y point.
-------------------------------------------