61A-002-Functions_6p..

The Elements of Programming
• Primitive Expressions and Statements
! The simplest building blocks of a language
• Means of Combination
! Compound elements are built from simpler ones
61A Lecture 2
• Means of Abstraction
Monday, August 29
! Compound elements can be named and manipulated as units
Programming languages allow us to communicate, too
2
Functions and Data
Types of expressions
Data: Stuff we want to manipulate
An expression
“The Art of Computer Programming”
describes a computation
2
Donald Knuth
(Ka-NOOTH)
This slide
and evaluates to a value
18 + 69
Count the words in a line of text
f (x)
Add numbers
√
100
�
Load the next slide
Pronounce someone’s name
sin π
6
23
Functions: Rules for manipulating data
3493161
�
i
i=1
| − 1869|
�
69
18
3
Call Expressions in Python
4
Anatomy of a Call Expression
add
Operator
(
2
Operand 0
,
3
)
Operand 1
Operators and operands are expressions
So they evaluate to values
All expressions can use function call notation
(Demo)
Evaluation procedure for call expressions:
1. Evaluate the operator and operand subexpressions
2. Apply the function that is the value of the operator
subexpression to the arguments that are the values of
the operand subexpression
5
6
Evaluating Nested Expressions
The Print Function
208
mul(add(2, mul(4, 6)), add(3, 5))
26
8
add(2, mul(4, 6))
add(3, 5)
mul
(Demo)
add
2
add
24
3
5
mul(4, 6)
mul
4
6
7
Pure Functions & Non-Pure Functions
-2
Nested Expressions with Print
None, None
Function signature: how many parameters
Pure Functions
abs(number):
2
8
Only produces
return values
print(...):
None
display “None None”
None
2, 100
print(print(1), print(2))
pow(x, y):
1267650600228229401496703205376
print
Non-Pure Functions
-2
print(...):
None
May create
side effects
1
display “-2”
None
None
print(1)
print(2)
print(...):
None
2
print(...):
display “1”
None
display “2”
9
Names and Assignment
10
Environments
Built-in
function
abs:
...
abs(x):
max:
...
Imported
value
(Demo)
Assigned
value
max(a, b, c, ...):
pi:
3.14...
tau:
6.28...
from math import pi
tau = 2 * pi
The environment does not track where names came from
11
12
Environments
User-Defined Functions
Named values are a simple means of abstraction
abs:
A frame holds
name bindings
Named expressions are a more powerful means of abstraction
abs(x):
...
max:
def expressions:
...
max(a, b, c, ...):
pi:
3.14
tau:
6.28
• Create a new function
• Bind a name to it
Binding to a
numeric value
from math import pi
tau = 2 * pi
>>> def <name>(<formal parameters>):
return <return expression>
The environment does not track where names came from
13
User-Defined Functions
14
Calling User-Defined Functions
Named values are a simple means of abstraction
mul:
Named expressions are a more powerful means of abstraction
mul(a,b):
...
square:
def expressions:
square(x):
• Create a new function
return mul(x, x)
• Bind a name to it
mul:
mul(a,b):
...
square:
>>> def square(x):
from operator import mul
def square(x):
return mul(x, x)
(Demo)
return mul(x, x)
15
Calling User-Defined Functions
16
Calling User-Defined Functions
• Bind formal parameters
• Eval return expression
mul:
...
mul(a,b):
square:
Environments
& Values
square(x):
return mul(x, x)
Expressions
The existing environment in which
the call expression is evaluated
x: -2
square
4
square(-2)
return mul(x, x)
from operator import mul
def square(x):
return mul(x, x)
square(-2)
17
The environment
created for the
function body
4
square(-2)
return mul(x, x)
from operator import mul
def square(x):
return mul(x, x)
square(-2)
18
Calling User-Defined Functions
Evaluating a Name in an Environment
mul:
A name evaluates
to the value
bound to that
name
mul:
mul(a,b):
...
...
square:
mul(a,b):
square:
square(x):
square(x):
return mul(x, x)
return mul(x, x)
x: -2
x: -2
square
square
4
square(-2)
return mul(x, x)
Points to an environment,
which starts with a frame
...in the
earliest frame
of the current
environment
...in which that
name is found
from operator import mul
def square(x):
return mul(x, x)
square(-2)
return mul(x, x)
19
Intrinsic Function Names Don’t Play a Role
20
Example: Function Application
Does not get
inspected
mul:
square:
sum_squares:
mul(a,b):
...
add, mul, ...
square(x):
return mul(x, x)
square:
sum_squares(x, y):
square(x):
return add(square(x), square(y))
return mul(x, x)
x: -2
Witness
Protection
Program
square
return mul(x, x)
Functions aren’t
accessed by their
intrinsic names!
def square(x):
return mul(x,x)
def sum_squares(x, y):
return add(square(x),square(y))
(Demo)
21
Example: Function Application
22
Example: Function Application
square:
square:
sum_squares:
sum_squares:
square(x):
add, mul, ...
add, mul, ...
return mul(x, x)
A
sum_squares(x, y):
x: 5
y: 12
x: 5
y: 12
return add(square(x), square(y))
sum_squares
square(x):
return mul(x, x)
sum_squares(x, y):
return add(square(x), square(y))
sum_squares
sum_squares(5,12)
sum_squares(5,12)
A add(square(x),square(y))
add(square(x),square(y))
return
def square(x):
return mul(x,x)
def sum_squares(x,y):
return add(...)
sum_squares(5, 12)
23
def square(x):
return mul(x,x)
def sum_squares(x,y):
return add(...)
sum_squares(5, 12)
24
Example: Function Application
Example: Function Application
square:
square:
sum_squares:
add, mul, ...
A
sum_squares:
square(x):
A
sum_squares(x, y):
x: 5
y: 12
square(x):
add, mul, ...
return mul(x, x)
return mul(x, x)
sum_squares(x, y):
x: 5
y: 12
return add(square(x), square(y))
sum_squares
return add(square(x), square(y))
sum_squares
169
x: 12
sum_squares(5,12)
square
A add(square(x),square(y))
x: 5
square
sum_squares(5,12)
A add(square(x),square(y))
x: 5
A
25
square(x)
mul(x, x)
def square(x):
return mul(x,x)
def sum_squares(x,y):
return add(...)
sum_squares(5, 12)
25
square
A
25
square(x)
mul(x, x)
A
144
square(y)
mul(x, x)
def square(x):
return mul(x,x)
def sum_squares(x,y):
return add(...)
sum_squares(5, 12)
26