02 Functions 1pp
02 Functions 1pp
Announcements
Expressions
Types of expressions
18 + 69 log2 1024
6 sin ⇡
23
2100 p
f (x) 3493161
100
X 1
7 mod 2 lim
i ✓ ◆ x!1 x
i=1
69
| 1869| 18
4
Call Expressions in Python
(Demo)
5
Anatomy of a Call Expression
add ( 2 , 3 )
6
Evaluating Nested Expressions
224
mul(add(4, mul(4, 6)), add(3, 5))
mul 28 8
add(4, mul(4, 6)) add(3, 5)
add 4 24 add 3 5
mul(4, 6)
mul 4 6
7
Evaluating Nested Expressions
add 4 24 add 3 5
mul(4, 6)
mul 4 6
Expression tree
8
Names, Assignment, and User-Defined Functions
(Demo)
Types of Expressions
10
Discussion Question 1
>>> f = min
>>> f = max
>>> max = g
???
11
Environment Diagrams
Environment Diagrams
Name Value
(Demo)
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
13
Assignment Statements
Just executed
Next to execute
Just executed
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
14
Discussion Question 1 Solution
(Demo)
func min(...) 3 4
f(2, g(h(1, 5), 3))
3
func max(...) 2 3
g(h(1, 5), 3)
func min(...) 5 3
h(1, 5)
func max(...) 1 5
15
http://pythontutor.com/composingprograms.html#code=f%20%3D%20min%0Af%20%3D%20max%0Ag,%20h%20%3D%20min,%20max%0Amax%20%3D%20g%0Amax%28f%282,%20g%28h%281,%205%29,%203%29%29,%204%29&cumulative=false&curInstr=0&mode=display&origin=composingprograms.js&py=3&rawInputLstJSON=%5B%5D
Defining Functions
Defining Functions
Function body defines the computation performed when the function is applied
2. Set the body of that function to be everything indented after the first line
17
Calling User-Defined Functions
Procedure for calling/applying user-defined functions (version 1):
1. Add a local frame, forming a new environment
2. Bind the function's formal parameters to its arguments in that frame
3. Execute the body of the function in that new environment
Built-in function
Original name of
function called
User-defined
Local frame function
Formal parameter
bound to argument
Return value
(not a binding!)
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
Calling User-Defined Functions
Procedure for calling/applying user-defined functions (version 1):
1. Add a local frame, forming a new environment
2. Bind the function's formal parameters to its arguments in that frame
3. Execute the body of the function in that new environment
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
19
Looking Up Names In Environments