0
Example code in âFunctional Programmingâ
I donât get this code. Why does the answer come out to be 20? If you pass the value from the first function to the second function, you donât get an answer of 20! Can someone please explain this to me?
5 Answers
+ 2
It does return 15 from the inner function:
add_five(add_five(10))
== add_five(10+5)
== add_five(15)
== 15+5
== 20.
+ 1
Are you talking about the code in the first slide?
https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2459/
0
yes... shouldnât this return 15 from the second funtion and then twice that, from the first function, for a total of 30?
what am i getting wrong?
0
got that... what about âapply_twiceâ ?
doesnt that function double the value returned from âadd_fiveâ?
seems to me that that would be 30...
0
You can do what's called substitution.
apply_twice is defined as `func(func(arg))`.
But then, when we call apply_twice, you can in your head substitute the arguments: `func` becomes `add_five` and `arg` becomes `10`.
So `func(func(arg))` becomes `add_five(add_five(10))` and then we are at where Diego Acero started off.