Tuesday 23 June 2020

20 - Python Question Discussion - 03


What is the output of the following program? Explain It.

def fun (n):

        if (n>100):

               return n-5

        return fun(fun(n+11))

print(fun(45))



(A)   50

(B)   100

(C)   74

(D)   Infinite Loop 


Explanation 

This is a recursive function(a function which call itself). So we should care about function calling and return values. Each time it return values as follows and final answer is 100. 


 

3 comments: