当前位置:首页 > 云计算 > 正文内容

python函数的返回值是什么

2022-05-04 03:00:00云计算3

返回值简介

函数需要先定义后调用,函数体中 return 语句的结果就是返回值。如果一个函数没有 reutrn 语句,其实它有一个隐含的 return 语句,返回值是 None,类型也是 'NoneType'。

return 语句的作用:

结束函数调用、返回值

指定返回值与隐含返回值

函数体中 return 语句有指定返回值时返回的就是其值

函数体中没有 return 语句时,函数运行结束会隐含返回一个 None 作为返回值,类型是 NoneType,与 return 、return None 等效,都是返回 None。

指定 return 返回值函数举例:

defshowplus(x):
print(x)
returnx+1

num=showplus(6)
add=num+2
print(add)

输出结果:

6
9

隐含 return None 举例:

defshowplus(x):
print(x)

num=showplus(6)
print(num)
print(type(num))

输出结果:

6
None
<class'NoneType'>

本网站文章仅供交流学习 ,不作为商用, 版权归属原作者,部分文章推送时未能及时与原作者取得联系,若来源标注错误或侵犯到您的权益烦请告知,我们将立即删除.

本文链接:https://www.xibujisuan.cn/2783.html

标签: Python