Note Custom functions: function names within a unified file cannot be the same.
Single return value
Syntax structure:
def function_name ([para1,para2 ...])
Code ...
Code ...
...
Return value--------------A value can be returned
eg
def test_c (n1,n2):-------------function definition
N=n1+n2
return n
Sum1=test_c (100,102)----------function call----It is best not to use sum because the sum is the keyword
print ' sum1= ', sum1
The sun1 is used to accept the return value of a function. The custom function has a return value, and a variable to hold or accept the return value when called
Multiple return values
eg
Syntax structure:
def function_name ([para1,para2 ...])
Code ...
Code ...
...
Return Value1,value2,value3:--------------can return multiple values, comma interval
eg
def test_d (N1,N2):
N=n1+n2
M=n1*n2
P=n1-n2
E=n1**n2
Return n,m,p,e
Sum1,multi,plus,pow=test_d (2,10)-----------preferably in this form
Print Sum1,multi,plus,pow
You must have the same number of variables to hold the return value of the function.
If the keynote function has only one variable, the amount of change is stored in tuples.
eg
Re=test_d (2,10)
Print RE
The results are as follows:
(12,20,-8,1024)-----parentheses surround the tuple
Print re[0],re[1],re[2],re[3]---------------elements used to print tuples
Eighth Python custom Function return value