The return value of the function:
Example 1:
def showplus (x):
Print (x)
return x + 1
Showplus (5)
The output is:
5
6
Example 2:
def showplus (x):
Print (x)
return x + 1
Print (x+1) #会执行吗?
Showplus (5)
The output is:
5
6
2, more than one return statement
def guess (x):
If x > 3:
Return "> 3"
Else
Return "<= 3"
Print (guess (10))
The output is:
3
def showplus (x):
Print (x)
return x + 1
return x + 2
Showplus (5)
The output is:
5
6
DEF fn (x):
For I in range (x):
If I > 3:
return I
Else
Print (' {} is not greater than 3 '. Format (x))
Print (FN (10))
Print (FN (3))
The output is:
4
3 is not greater than 3
None
Return multiple values???
Def showlist ():
return [All-in-one]---returns a list that is a list object
Def showlist ():
Return-in-a----appears to return multiple values implicitly being encapsulated by Python as a tuple.
Summarize:
The Python function returns the return value using the return statement
All functions have a return value and, if there is no return statement, implicitly calls return None
The return statement is not necessarily the most likely statement of a function's statement block
A Hansu can have multiple return statements, but only one can be executed, and if no return statement is executed, an implicit call to return None
If necessary, you can display the call return None, which can be shortened to return
If the function executes the return statement, the function returns, and the other statements after the currently executed return statement are not executed.
Effect: Ends a function call, returns a value.
Scope:
The visible range of an identifier, which is the scope of the identifier. Generally speaking, the scope of a variable
Global scope:
Visible throughout the program's running environment
Local scope:
Visible inside functions, classes, etc.
Local variables cannot be used beyond the local scope in which they reside.
Python function return value, scope