+ 3

I dont get it

so i saw this on 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)) i dont get what does func do

2nd Mar 2017, 9:29 AM
Mask
Mask - avatar
6 ответов
+ 12
func represents the formal parameter for functions. In this case, function add was used in the parameters, hence function do_twice returns: add(add(5, 10), add(5, 10)) resulting in return value of 30.
2nd Mar 2017, 9:39 AM
Hatsy Rei
Hatsy Rei - avatar
+ 12
@Meharban You can replace 'func' with any other legit variable name you prefer.
2nd Mar 2017, 12:03 PM
Hatsy Rei
Hatsy Rei - avatar
+ 3
@Hasty, can we use any other variable as parameter instaed of func?
2nd Mar 2017, 10:22 AM
Meharban Singh
Meharban Singh - avatar
+ 1
thanks so much :)
2nd Mar 2017, 9:40 AM
Mask
Mask - avatar
+ 1
thanks@Hasty
2nd Mar 2017, 12:07 PM
Meharban Singh
Meharban Singh - avatar
0
You can see it as a flag. It is used to receive which function should be used
3rd Mar 2017, 6:51 AM
Jonyhy
Jonyhy - avatar