Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Adspython
explore-python
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
explore-python
/
Function
/
func_definition.md
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
53 lines (37 loc) · 1.07 KB
master
Breadcrumbs
explore-python
/
Function
/
func_definition.md
Copy path
Top
File metadata and controls
Code
Blame
53 lines (37 loc) · 1.07 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#
定义函数
在 Python 中,定义函数使用
`
def
`
语句。一个函数主要由三部分构成:
-
函数名
-
函数参数
-
函数返回值
让我们看一个简单的例子:
```
python
def
hello
(
name
):
return
name
>>
>
r
=
hello(
'
ethan
'
)
>>
>
r
'
ethan
'
```
在上面,我们定义了一个函数。函数名是
`
hello
`
;函数有一个参数,参数名是 name;函数有一个返回值,name。
我们也可以定义一个没有参数和返回值的函数:
```
python
def
greet
():
#
没有参数
print
'
hello world
'
#
没有 return,会自动 return None
>>
>
r
=
greet()
hello world
>>
>
r
==
None
```
这里,函数
`
greet
`
没有参数,它也没有返回值(或者说是 None)。
我们还可以定义返回多个值的函数:
```
python
>>
>
def
add_one
(
x
,
y
,
z
):
...
return
x
+
1
, y
+
1
, z
+
1
#
有 3 个返回值
...
>>
>
>>
>
result
=
add_one(
1
,
5
,
9
)
>>
>
result
#
result 实际上是一个 tuple
(
2
,
6
,
10
)
>>
>
type
(result)
<
type
'
tuple
'
>
```
#
小结
-
如果函数没有
`
return
`
语句,则自动
`
return None
`
。
You can’t perform that action at this time.