+ 2

can somebody explain this question to me in easy language

def add(x, y): return x + y def do_twice(func, x, y): return func(func(x, y), func(x, y)) a = 5 b = 10 print(do_twice(add, a, b))

29th Jan 2022, 11:09 AM
NIKHIL
4 Réponses
+ 3
Ya sure NIKHIL def add(x ,y): return x + y You have created a function name add with 2 parameters and you are returning the sum of those 2 def do_twice(func, x, y): return func(func(x, y), func(x, y)) You have created a function called do twice which accepts 3 parameters [ IMPORTANT ] How to assign a function in a variable >>> p = print #note we haven't called function by using () >>> >>> p("Hello, World") Hello, World >>> k = list >>> k() [] Now same with your case, a = 5 b = 10 print(do_twice(func=add, x=a, y=b)) in your function now whenever you write func() it will only call add, means its similar to add(add(5, 10), add(5, 10)) add(15, 15) = 30 Hope it's helpful! Happy Learning 🙂 Example: https://code.sololearn.com/cM8gk3xKaF5F/?ref=app
29th Jan 2022, 11:24 AM
Abhiyantā
Abhiyantā - avatar
29th Jan 2022, 3:21 PM
A͢J
A͢J - avatar
- 1
You really don't need to understand this code, cz you can double the value in multiple ways, i always prefer to use while and for loops. because functions are bit slow
31st Jan 2022, 2:31 AM
Saad Khan
Saad Khan - avatar