I expected 'return x' to be 1 but None was returned. Why?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I expected 'return x' to be 1 but None was returned. Why??

def fuc(x): if x==1: return x else: x-=1 fuc(x) x=2 print(fuc(x),x)

28th Apr 2021, 7:46 AM
Gbadegesin Muhammed
Gbadegesin Muhammed - avatar
2 Answers
+ 5
On line 6, you just call fuc(x), you need to return the result as well by doing return fuc(x).
28th Apr 2021, 7:52 AM
Shahil Ahmed
Shahil Ahmed - avatar
+ 3
Good question and your expectation is acceptable. Recursion needs a return. And it is a bit complicated to explain why.
28th Apr 2021, 7:59 AM
Oma Falk
Oma Falk - avatar