def 函数名([参数列表]):
['''文档字符串''']
函数体
[return语句]
1.定义练习
def add():
result = 11 + 22
print(result)
def add_2(a,b):
result = a + b
print(result)
def add_3(a,b):
result = a + b
print(result)
return result
#c = a 函数内的参数不能在外部事宜
2.调用练习
#函数定义以后不会运行,调用才会运行
add()
add_2(a=5,b=9)
add_2(10,1)
add_2(b=10,a=2)
re3 = add_3(10,3)
函数外部无法直接调用内层函数,只能在外层函数中调用内层函数
#---------------------嵌套扩展---------------------
def add_modify(a,b):
result = a + b
print(result)
def test():
print("我是内层函数")
test()
add_modify(10,20)
位置参数
#----------------------函数的参数传递------------------
def get_max(a,b):
"""
获取2个数之间的最大值
"""
if a>b:
print(a,"是较大值!")
else:
print(b,"是较大值")
get_max(10,5)
运行效果