3. 函数
1. 定义函数
def function_name(paramaters):
statement1
statement2
statement3
def hello():
print("Hello")
def hello(name):
print("Hello!" + name)
2. Python的函数不能重载
3. 定义有返回值的函数
def function_name(paramaters):
statement1
statement2
statement3
return values
(1) 单值返回
def add(a, b):
return a + b
add(3, 4)
(2) 多值返回
def test(a, b):
c = a + b
d = a - b
e = a * b
return c, d, e
a, b, c = test(3, 4)
re = test(3, 4)
re[0] re[1] re[2]
(3) 参数预设值
预定义值必须靠在右边
def test(a, b = 2):
c = a + b
return c
test(1)
test(1, 5)
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。