0

I need help

Example 1. def fun(x): if x == 0: return 0 return x + fun(x-1) print(fun(3)) Output: 6 Example 2. def apply_twice(func, arg): return func(func(arg)) def add_five(x): return x + 5 print(apply_twice(add_five, 10)) Output: 20 I need to ask why not function value add in x in 1 example while in 2 examples function values save then it will be argument of another function. Please guide me....

3rd Aug 2019, 6:31 AM
Yasir Muhammad
Yasir Muhammad - avatar
4 Réponses
3rd Aug 2019, 7:15 AM
Mikhail Gorchanyuk
Mikhail Gorchanyuk - avatar
+ 1
The recursive function in example 1 works correctly. The calculation performed is 3+2+1+0 = 6. The calculation in example 2 ist also correct: (10+5)+5 = 20.
3rd Aug 2019, 6:47 AM
Michael
Michael - avatar
0
Michael i understand how the answer is ganerted but i need to ask => return x + fun(x-1) => return 3 + fun(2) why not add value of fun in x => return 3 + Value of fun(2)
3rd Aug 2019, 6:57 AM
Yasir Muhammad
Yasir Muhammad - avatar
0
You can see code running step by step on http://pythontutor.com/visualize.html And mutable of variables also you can see For my learning it's big help
7th Aug 2019, 8:18 PM
Сергей Бойко
Сергей Бойко - avatar