tip:可以先学数据结构和算法
返回值 将函数中运算的结果通过return关键字“扔”出来
没有返回值的情况:
def func():
print('---')
x=func()
print(x)
'''
---
None
'''
def add(a,b):
result = a+b
#print(result)
return result
#print(result) NameError: name 'result' is not defined
函数体中的print仅限于在控制台上出现,告诉你一下,但不能把结果拿出来
扔到哪去了? 用变量去接return的返回值
def add(a,b):
result = a+b
print(result)
return 'aaa',100
x = add(2,6)
print(x)
x,y = add(2,6)
print(x,y)
'''
8
('aaa', 100)
aaa 100
'''
return返回值
1.return后面可以是一个参数,接收的时候x=add(1,2)
2.return后面也可以是多个参数,会将多个参数先放在一个元组中,将元组作为整体返回
3.接收的时候也可以是多个,return 'hello','world' x,y=('hello','world') --->x='hello' y='world'
4.没有返回值,接收到的是None
函数嵌套调用 嵌套函数扔出来即闭包 闭包