您现在的位置是:课程教程文章

python函数的返回值是什么

2023-12-18 23:03课程教程文章 人已围观

返回值简介

函数需要先定义后调用,函数体中 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'>
课程教程:python函数的返回值是什么

上一篇:python判断是否是目录

下一篇:没有了

站点信息

  • 文章统计篇文章