"""
def 函数名(形参):
pass
"""
def add(x, y):
x+y
result1 = add(11, 12)
print("result1的值为:%s"% result1)
print("*"*50)
'''
匿名函数 (执行一个简单的表达式)
形式:变量 = lambda 参数:表达式
匿名函数默认自带 return
'''
func = lambda x, y: x + y
result=func(11, 12)
print("result的值为:%s"% result)
输出结果:
匿名函数是由关键字 lambda 声明的,并且,匿名函数默认自带 return 返回值
在这里如果将第一个print()中的 %s改为 %d即将以字符串输出改为数字输出,会有一个有趣的现象:
def add(x, y):
x + y
result1 = add(11, 12)
print("result1 的值为:%d"% result1)
结果:
数字类型不能为 None
匿名函数的用法实例:
def test(a,b,func):
result=func(a,b)
print(result)
test(11,22,lambda x,y:x+y)
将匿名函数做为参数传递到 test() 函数中,输出结果为 33