python的返回值簡介
函數需要先定義后調用,函數體中return語句的結果就是返回值。如果一個函數沒有reutrn語句,其實它有一個隱含的return語句,返回值是None,類型也是'NoneType'。
return語句的作用:
結束函數調用、返回值
指定返回值與隱含返回值
函數體中return語句有指定返回值時返回的就是其值
函數體中沒有return語句時,函數運行結束會隱含返回一個None作為返回值,類型是NoneType,與return、returnNone等效,都是返回None。
指定return返回值函數舉例:
defshowplus(x):
print(x)
returnx+1
num=showplus(6)
add=num+2
print(add)
輸出結果:
6
9
隱含returnNone舉例:
defshowplus(x):
print(x)
num=showplus(6)
print(num)
print(type(num))
輸出結果:
6
None
以上內容為大家介紹了python培訓之函數的返回值是什么,希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構:千鋒教育。