Why this function is behaving like a loop
def factorial(no):
if no == 0:
return 1
else:
return no * factorial(no - 1)
print("factorial of a number is:", factorial(8))
why this behaves like a loop, it thought it would return no*factorial(no-1) as 8*7.
And when I replace return with print() function it shows errror
def factorial(no):
if no == 0:
return 1
else:
print(no * factorial(no - 1))
factorial(8)
•ERROR
1
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
start(fakepyfile,mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), __main__.__dict__)
File "<string>", line 7, in <module>
File "<string>", line 5, in factorial
File "<string>", line 5, in factorial
File "<string>", line 5, in factorial
[Previous line repeated 4 more times]
TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
[Program finished]
Can someone explain how print() and return work differently in function