Hi Be grateful if someone could assist with me this Python q

Hi, Be grateful if someone could assist with me this Python question on Functions. Thanks

Consider the following function:

def what(n):

if n == 0:

return 0

else:

return n + what(n-1)

What does it do? Whats the result returned by what(3)? What happens when we attempt to evaluate what(-3)?

Solution

def what(n):

if n == 0:

return 0

else:

return n + what(n-1)

It will add numbers from 0 to n i.e.if n=3 it does 3+2+1+0=6

What happens when we attempt to evaluate what(-3)?

Ans:It will go to recursion and stack will become full because n never becomes 0

Hi, Be grateful if someone could assist with me this Python question on Functions. Thanks Consider the following function: def what(n): if n == 0: return 0 else

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site