0

Can someone please help me explain this code.

x = 5 def fun(x): x = x - (x-2) return x print(fun(fun(fun(x-1)))) Three questions: Is this a form of recursion? On line 5. Does the amount of ‘(fun)’ make any difference? Because no matter how many ‘fun’ I put I get the same output which is 2 Does the code end after going through function the 1st time or does it end after the 3rd time (because there are 3 fun’s)?

25th Sep 2021, 7:33 PM
Jenkins
1 Antwort
+ 2
x-(x-2) = 2 Therefore fun() will always return the value 2. The function is called three times. The first time with 4 (x-1), the other times with 2.
25th Sep 2021, 7:36 PM
Simon Sauter
Simon Sauter - avatar