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))
3 Respuestas
+ 2
oh, yes i got it.thank you so much.
+ 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.
+ 2
You're more than welcome bro! Best of luck with your learning.