0
What is the easy way to understand it.... don't share code just tell me the correct guideline of understanding it. Please
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))
4 Réponses
+ 7
To understand it in easy way:
def add(x, y): # Defines a function that takes two arguments and add them.
return x + y #It returns the sum of x and y
def do_twice(func, x, y): # This also defines a new function that takes 3 arguments i.e are function and two values x and y.
return func(func(x, y), func(x, y))
# Its working is explained where it is called i.e below.
a = 5 # a is assigned the value 5
b = 10# b is assigned the value 10
print(do_twice(add, a, b))
add:Adds the given two arguments.
do_twice: add(add(5,10),add(5,10))
add(15,15)
which is 30.
Note: Remember to see comments in the lesson and search bar too
+ 4
Thank u all cleared I'll now try it to practice.
+ 3
You need to treat functions as values, like integers and strings.
You can assign them to variables, store in lists, use as arguments for other functions, but printing them may not give nice output.
When ever you follow the value with parentheses, the function will be called.