0

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)) Please explain step by step Which is caller here? Explain the 3rd and 4th line ? why in 4th line we write add(add(x,y),add(x,y)) which is function of 1st line ? how can we write function after return statement insted of (x+y+x+y )=30?

29th Jul 2016, 7:22 PM
Rupam Das
Rupam Das - avatar
2 Answers
+ 2
we first call the function do_twice and we pass 3 variables - add (the add function), a (having the value of 5), b (having the value of 10) 3rd line defines a function do_twice accepting 3 arguments (func, x, y) since we called the function, those arguments would have the values we passed on to it. the 4th line - will then become add(add(x,y), add(x,y)) we will then first evaluate the 2 inner function calls add(x,y) -> add(5,10) which will evaluate to the value of 15. so now that we have evaluated the inner function calls' values we now have the outer function call which now looks like - add(15, 15) so evaluating that we get 30. as for your last question I don't quite understand it.
1st Aug 2016, 2:58 AM
Philippe Oscar Sanoy
Philippe Oscar Sanoy - avatar
0
def shout(word): return word + "!" speak = shout output = speak("shout") print(output)
30th Sep 2019, 3:59 PM
William Mcquillen
William Mcquillen - avatar