To understand how a code works, just follow the functions.
foo
is called. Since integer
types are immutable (wikipedia) in foo(x,y)
x
and y
are copies of the x
and y
you gave as parameters.
In python expression are evaluated from left to right therefore.
First function hoo
is called (same remark about parameters) the value of x
and y
are changed and z
is computed you get ('x =', 5, '; y =', 6, '; z =', 11)
and the value z
(11) is returned.
Once outside hoo
function x
and y
at this level are respectively 10 and 20 because they were not changed (immutable thing… see above) . Then function goo
is called. (same remark about parameters) (warning you enter y
for parameter x
of goo
and x
for parameter y
of goo
) the value of x
and y
are changed and z
is computed you get ('x =', 40, '; y =', 30, '; z =', 70) and the value z
(70) is returned.
Once outside goo
function x
and y
at this level are respectively 10 and 20 because they were not changed (immutable thing… see above).
The sum function between the two values returned by hoo
and goo
is done. z
= 11 + 70. you get ('x =', 10, '; y =', 20, '; z =', 81) and z is returned.
Then outside foo
function. x
and y
at this level are respectively 10 and 20 because they were not changed (immutable thing… see above) . z is given the value of the return function foo
therefore z
=81
And you get ('x =', 10, '; y =', 20, '; z =', 81)