0

Why is the word 'func' used here? Can anyone explain me this program.

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))

21st Sep 2017, 4:05 PM
Paavan Gupta
Paavan Gupta - avatar
3 Respostas
+ 2
oh, yes i got it.thank you so much.
21st Sep 2017, 4:10 PM
Paavan Gupta
Paavan Gupta - avatar
+ 2
It's just a variable that's storing which function you're wanting to use. In this case, you're wanting to use the function add in the do_twice function. Get what I mean? Look at how do_twice is defined as a function. It parameters are func, x, and y. So you feed it which func (in this case it's add), and then the x & y numbers. If I converted the variable to its value, this is what it looks like in THIS scenario: def do_twice(add, x, y): return add(add(x, y), add(x, y)) We decided it would be add in this line: do_twice(add, a, b) Hope that helps.
21st Sep 2017, 4:10 PM
AgentSmith
+ 2
You're more than welcome bro! Best of luck with your learning.
21st Sep 2017, 4:11 PM
AgentSmith