+ 2

Functional programming - Help

I'm just into the first lesson in the functional programming section and can't understand the first code, def apply_twice(func, arg): return func(func(arg)) def add_five(x): return x + 5 print(apply_twice(add_five, 10)) How did the result turn out 20 and what is #return func(func(arg)) Can anyone explain this in a simple way Thank you Have a great day!

15th Jan 2023, 9:07 AM
Karim Maasarani
Karim Maasarani - avatar
2 Respostas
+ 5
The code is defining two functions: apply_twice and add_five. The apply_twice function takes two arguments, a function (func) and an argument (arg). It then calls the function twice, passing in the argument each time. The add_five function takes one argument (x) and adds 5 to it. When you call apply_twice with add_five and 10 as arguments, it calls add_five twice with 10 as the argument each time. So the result is 10 + 5 + 5 = 20.
15th Jan 2023, 9:09 AM
Reza Mardani
Reza Mardani - avatar
+ 3
im Reza thanks alot!!!!
15th Jan 2023, 9:57 AM
Karim Maasarani
Karim Maasarani - avatar