What does add do here? Why is it here? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What does add do here? Why is it here?

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))

27th May 2020, 3:37 PM
3.14
3.14 - avatar
2 Answers
+ 3
This is more of a demonstration of what can be done than a useful piece of code. It demonstrates that you can not only pass a string, number, list, etc. to a function, but you can also pass a function name to it. add() is just a very simple function that is being used to demonstrate this In do_twice(), the func parameter becomes the name of the function passed to it, in this example, add(). So the return line becomes return add(add(a, b), add(a, b)) which is return add(15, 15) so it returns 30. Again, nothing special about add() just a simple function to demonstrate what's possible.
27th May 2020, 5:08 PM
Russ
Russ - avatar
0
27th May 2020, 5:16 PM
Josshual A. Toro M.
Josshual A. Toro M. - avatar