Python函数
01、创建函数
# 创建函数语法
def funaction_name(parameters):
"""docstring"""
statements(s)
02、调用函数
name = "Tom"
#定义函数
def greet(name):
"""This function greets to
the person passed in as
parameter"""
print("Hello," + name + ",Good morning!")
# 调用函数
greet(name) # Hello,Tom,Good morning!
03、文档字符串
-
函数头之后的第一个字符串称为docstring,是文档字符串的缩写,其用于简要解释函数的作用 。
-
通常使用三引号将文档字符串框选,我们将此字符串作为函数的__doc__属性 使用。
def greet(name):
"""This function greets to
the person passed in as
parameter"""
print("Hello," + name + ",Good morning!")
# 输出此字符串
print(greet.__doc__)
"""
This function greets to
the person passed in as
parameter
"""
04、返回语句
- return语句用于退出函数并返回到调用函数的位置 。
- 与ja