def nat_fn1(k):
if k == 0:
return 1
elif k%2 == 0:
return 2
else:
return nat_fn1 (k-1)
I first tried nat_fn1(5)
and I got an answer of 2. This was expected. Then I tried the same code without return, I got a value of None, this was expected too. So applying the same concept, I tried this code :
def process_strings(s,t):
if s == "":
if not(t== ""):
print(t)
else:
print("{0}{1}".format(s[0],t[0]))
process_strings(s[1:], t[1:])
I tried process_strings("cat","dog")
, expecting None as there is no return in the recursion. However, I still got an answer of :
cd
ao
tg
I understand how it got these values. But there's no return statement. Why is this working?