Functions | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Functions

Please explain how the following results (30) are coming? Total example page is given copped from the web. ................................. Functions can also be used as arguments of other functions. 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)) Try It Yourself Result: >>> 30 >>> As you can see, the function do_twice takes a function as its argument and calls it in its body.

28th Dec 2019, 9:44 AM
Thilina Prasad Jayarathna
Thilina Prasad Jayarathna - avatar
2 Answers
+ 5
You are giving add to the function do twice. So do_twice actually calculates add(add(5, 10) + add(5,10)) meaning add(15, 15) -> 30
28th Dec 2019, 9:50 AM
HonFu
HonFu - avatar
+ 1
Thank you HonFu
28th Dec 2019, 10:32 AM
Thilina Prasad Jayarathna
Thilina Prasad Jayarathna - avatar