function code snippet that has function calls I have never seen before. How does it work.

Steven D'Aprano steve at pearwood.info
Sun Oct 4 09:42:31 EDT 2015


On Sun, 4 Oct 2015 04:40 am, Ronald Cosentino wrote:

> def funA(x,y,z):
>     return (x+y) * z
> def funB(x,y):
>     return(x-y)
> print(funA(4,funB(2,3), funB(3,2)))
> 
> the answer is 3. I don't know how it works.


Break it up and consider it a little at a time, starting with the three
values given to funA:

* calculate 4 (too easy, it's already done)

* calculate funB(2, 3)
  => funB(2, 3) returns 2-3, which gives -1

* calculate funB(3,2)
  => funB(3, 2) returns 3-2, which gives 1


Then pass those three values to the funA function:

* calculate funA(4, -1, 1)
  => which returns (4 + -1)*1, which gives 3


and finally pass that value to print:

* print(3)


which prints 3.




-- 
Steven




More information about the Python-list mailing list