+ 1

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, 10:31 AM
Rupam Das
Rupam Das - avatar
6 Respostas
+ 4
see..def add(x,y): #you defined your function here. #in which x and y are parameters. return x+y #a return statement. #after it not a statement will get executed. def do_twice(func,x,y): return func(func(x,y),func(x,y)) #another func. is defined.but,'func' is the difference. #'func' is used as a parameter to call a func. later. a=5 b=10 #let's skip it. print(do_twice(add,a,b)#a function print. #in which,do_twice is 'called'. #and add,a,b are the arguments for parameters. #thus,do_twice will get executed.as defined. #return add(add(5,10),add(5,10)) #add (5,10)=15.thus,add(15,15)=30. #we have return statement.thus,we can use this .. #value outside the def. func. too.it will return 30 #which will print 30. note- after 'return' statement nothing is executed.
31st Jul 2016, 5:17 PM
sushant
sushant - avatar
+ 1
No i was not asking that. Please read carefully what i have written below
29th Jul 2016, 7:16 PM
Rupam Das
Rupam Das - avatar
0
какието непонятные примеры кода и без обьяснения логики - удаляю приложение
10th Nov 2016, 9:59 PM
Alex Guwa
Alex Guwa - avatar
0
why this func is adding the (a,b) do_twise func (func(x,y),func(x,y)) if i remove the func it's print result 5,10,5,10
3rd Apr 2020, 3:42 AM
Ananya Gupta
Ananya Gupta  - avatar
0
Can someone help me with this def add_it(..... ,..... ) return.......... print(add_it(5,6)) I need to fill the Voids with this y , 6, x, z, 11, 5 Thanks for everything 🙂 y , 6, x, a,11,5,
7th Apr 2020, 11:54 AM
Mika Hiakuia
Mika Hiakuia - avatar
- 1
Maybe you're asking "Recursion" add(add(5,10),add(5,10) ->add(15,15) ->30 OR ans=0 for i in range(2): ans += add(a,b)
29th Jul 2016, 1:43 PM
jimmy