Python function return value, scope

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.