尾遞歸-python

尾遞歸:指最後一步(不一定是最後一行)調用另一個函數

#函數test01在test02中爲尾調用
def test01(x):
    print(x)
def test02(y):
    y+=1
    print(y)
    return test01(x)  #程序執行到這裏,test02的狀態將不會保留,因爲跳轉到test01(x)時已是最後一步



#函數test01在test02中爲零尾調用
def test01(x):
    return x
def test02(y):
    y+=1
    print(y)
    return test01(x)+1   #程序執行到這裏,test02的狀態將會保留,因爲跳轉到test01(x)時已是最後一步
    

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章