python函数的返回值是什么

764次阅读
没有评论

python函数的返回值是什么

返回值简介

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

return 语句的作用:

结束函数调用、返回值

指定返回值与隐含返回值

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

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

指定 return 返回值函数举例:

def showplus(x):
    print(x)
    return x + 1
     
num = showplus(6)
add = num + 2
print(add)

输出结果:

6
9

隐含 return None 举例:

def showplus(x):
    print(x)
 
num = showplus(6)
print(num)
print(type(num))

输出结果:

6
None
<class 'NoneType'>
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2019-12-04发表,共计483字。
新手QQ群:570568346,欢迎进群讨论 Python51学习
评论(没有评论)