Professional Documents
Culture Documents
04-Environments 4pp
04-Environments 4pp
04-Environments 4pp
Types of expressions
18 + 69 log2 1024
6 sin ⇡
Expressions
23
2100 p
f (x) 3493161
100
X 1
7 mod 2 lim
i ✓ ◆ x!1 x
i=1
69
| 1869| 18
(Demo)
4
Anatomy of a Call Expression Evaluating Nested Expressions
add ( 2 + 2 , mul(3, 4) )
224
Operator Operand Operand mul(add(4, mul(4, 6)), add(3, 5))
mul(4, 6)
1. Evaluate the operator and then the operand subexpressions
5 6
add 4 24 add 3 5
(Demo)
mul(4, 6)
mul 4 6
Expression tree
7
None Indicates that Nothing is Returned Pure Functions & Non-Pure Functions
2, 100 pow
>>> def does_not_return_square(x): 1267650600228229401496703205376
... x * x 2 Arguments
No return
...
None value is not displayed Returns None!
>>> does_not_return_square(4) Non-Pure Functions
have side effects -2 print
The name sixteen >>> sixteen = does_not_return_square(4) None
is now bound to A side effect isn't a
the value None >>> sixteen + 4 A non-pure value; it's anything
Traceback (most recent call last): function doesn't Python displays the output “-2” that happens as a
have to return consequence of
File "<stdin>", line 1, in <module> None (but print calling a function
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' always does).
9 10
11
Environment Diagrams
(Demo: tutor.cs61a.org )
http://pythontutor.com/composingprograms.html#code=from%20math%20import%20pi%0Atau%20%3D%202%20*%20pi&cumulative=false&curInstr=1&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D
14
Assignment Statements
Just executed
Next to execute
Calling Functions
Just executed
(Demo: tutor.cs61a.org )
1. Evaluate all expressions to the right of = from left to right.
2. Bind all names to the left of = to those resulting values in the current frame.
http://pythontutor.com/composingprograms.html#code=a%20%3D%201%0Ab%20%3D%202%0Ab,%20a%20%3D%20a%20%2B%20b,%20b&cumulative=false&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D
15
http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28-2%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D
Calling User-Defined Functions Calling User-Defined Functions
Procedure for calling/applying user-defined functions (version 1): Procedure for calling/applying user-defined functions (version 1):
1. Add a local frame 1. Add a local frame
2. Bind the function's formal parameters to its arguments in that frame 2. Bind the function's formal parameters to its arguments in that frame
3. Execute the body of the function in that new environment 3. Execute the body of the function in that new environment
Built-in function
Original name of
function called
User-defined
Local frame function
http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28-2%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D
17 http://pythontutor.com/composingprograms.html#code=from%20operator%20import%20mul%0Adef%20square%28x%29%3A%0A%20%20%20%20return%20mul%28x,%20x%29%0Asquare%28-2%29&cumulative=true&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D
18
1
81
square(square(3)) square(square(3))
1
Every expression is
evaluated in the context
of an environment.