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

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)) let me know how does the code works.....

17th Apr 2020, 9:14 AM
PRIYANKA K
PRIYANKA K - avatar
1 Answer
0
do_twice in the print statement is called first. func = add, x=a, y=b So the return statement in the do_twice will be add(add(5,10),add(5,10)) The inner functions will be called first and the add returns 15 for both functions. Then it will be add(15,15) Finally it will add those two and return 30. 30 is returned to the print statement and printed
17th Apr 2020, 10:38 AM
Abirame Dhevendran
Abirame Dhevendran - avatar