+ 1
Exaplain the below code
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 odpowiedzi
+ 5
Begin from end:
Do_twice gets a func, x and y
func =add
X,y = 5,10
It returns add(add(5,10),add(5,10))
= add(15,15)=30
The prog demonstrates that a function can be a parameter like integer.
+ 1
It's calling function inside function by passing function inside it.
Let's begin from the last line and go from bottom to top.
print(do_twice(add,a,b))
Here do_twice function will be called by paasing three values.
First is func, which is function name,
Second a and third b are values.
Now in definition of do_twice,
The function add is called by passing output of two more function calls.
Let's understand the line-
return func(func(x,y) , func(x,y))
here func() means to call add() function defined at top.
first we are calling the function with values 5,10 twice and passing the result again in first and second parameter.
It will look like -
do_twice(add, 5, 10)
func(func(5,10),func(5,10))
func(15,15)
30
So the output 30 will be printed.
I hope you did not get confused here. 😅
0
Function msg ( )
{
do. While( 12.5*14.0) ;
goString(x+y) ;
}
THIS IS C++ PROGRAM. SHARE WITH ALL YOUR FRIENDS.