Lecture 2 - Names & Functions

Chris Allsman

Announcements

  • HW0 released, due Friday
    • A survey, but get it done early so you don’t forget!
  • Tentative dates for assignments:
    • HW1 released tomorrow, due Tuesday
    • Proj1 released Thursday, due July 8
  • Discussion/OH starts today
    • If you’re in Disc 107, room has been changed to Wheeler 130
  • Lab starts tomorrow or Thursday
    • Attend the lab matching with your discussion section (discussion # - 90, see staff pages for rooms/times)
  • Small-group tutoring signups out by end of week!

Policies, Part II

Communication

  • You should use piazza or reach out to your section TA for 99% of questions/issues
    • In particular, questions about assignments belong on piazza most of the time
    • Join https://piazza.com/berkeley/summer2019/cs61a if you haven’t been added!
  • You will be expected to keep up with piazza announcements, but you may wish to turn off email notifications
    • Settings (gear icon in top right) -> Account/Email Settings
    • Will still get emails for announcements

Mental Health/DSP Accommodations

  • We (the instructors) have access to DSP letters submitted, and will reach out closer to the exams for accomodations.
    • For accomodations related to assignments, contact us at ccs61a+su19@berkeley.edu
  • Resources for everyone:
    • CAPS: (510) 642-9494
    • DSP Office: dsp.berkeley.edu
    • Reach out to your TA prior to an assignment deadline if extenuating circumstances make it difficult to complete

Program Structure

Review - Expressions

Primitive Expressions:

Call Expressions:

Arithmetic Expressions:

2

“hello!”

add

add(3, 4)

max(add(2, 3), 5 * min(-1, 4))

1 + 2

15 // 3

numbers

strings

names

Review - Evaluating Call Expressions

  • Evaluate
    • Evaluate the operator subexpression
    • Evaluate each operand subexpression
  • Apply
    • Apply the value of the operator subexpression to the values of the operand subexpression

add ( 2 , 3 )

Operator

Operand

Operand

Values

Programs manipulate values

Values represent different types of data

Floats:

Integers:

Strings:

Booleans:

2

44

-3

3.14

4.5

-2.0

“cs61a”

“hello!”

False

True

Expressions & Values

Expressions evaluate to values in one or more steps

Expression:

Value:

‘hello!’

7 / 2

3.5

add(1, max(2, 3))

4

‘hello!’

Names

Values can be assigned to names to make referring to them easier.

A name can only be bound to a single value.

One way to introduce a new name in a program is with an assignment statement.

Statements affect the program, but do not evaluate to values.

Demo

x = 1 + 2 * 3 - 4 // 5

Name

Expression

7

x

Check Your Understanding

>>> f = min

>>> f = max

>>> g, h = min, max

>>> max = g

>>> max(f(2, g(h(1, 5), 3)), 4)

???

Visualizing Assignment

Names are bound to values in an environment

To execute an assignment statement:

  • Evaluate the expression to the right of =.
  • Bind the value of the expression to the name to the left of = in the current environment.

Final Value

Bindings

Name

Demo

http://pythontutor.com/visualize.html#code=x%20%3D%201%0Ay%20%3D%20x%0Ax%20%3D%202%0Ax,%20y%20%3D%20x%20%2B%20y,%20x&cumulative=true&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false

func min(...)

f(2, g(h(1, 5), 3))

func max(...)

2

g(h(1, 5), 3)

func min(...)

h(1, 5)

3

func max(...)

1

5

5

3

3

4

3

3

Demo

http://pythontutor.com/visualize.html#code=f%20%3D%20min%0Af%20%3D%20max%0Ag,%20h%20%3D%20min,%20max%0Amax%20%3D%20g%0A&cumulative=true&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false

Break

Functions

Functions

Functions allow us to abstract away entire expressions and sequences of computation

They take in some input (known as their arguments) and transform it into an output (the return value)

We can create functions using def statements. Their input is given in a function call, and their output is given by a return statement.

square

5

25

Defining Functions

def <name>(<parameters>):
return <return expression>

Function signature indicates name and number of arguments

Function body defines the computation

performed when the function is applied

def square(x):
return x * x
y = square(-2)

Execution rule for def Statements

  • Create a function with signature <name>(<parameters>)
  • Set the body of that function to be everything indented after the first line
  • Bind <name> to that function in the current frame

Demo

Functions in Environment Diagrams

def statements are a type of assignment that bind names to function values

Built-in function

User-defined function

Calling User-Defined Functions

Procedure for calling/applying user-defined functions (for now)

  • Create a new environment frame
  • Bind the function's parameters to its arguments in that frame
  • Execute the body of the function in the new environment

def square(x):

return x * x

square(-2)

Calling User-Defined Functions

Procedure for calling/applying user-defined functions (for now)

  • Create a new environment frame
  • Bind the function's parameters to its arguments in that frame
  • Execute the body of the function in the new environment

def square(x):

return x * x

square(-2)

Local frame

Intrinsic name

Calling User-Defined Functions

Procedure for calling/applying user-defined functions (for now)

  • Create a new environment frame
  • Bind the function's parameters to its arguments in that frame
  • Execute the body of the function in the new environment

def square(x):

return x * x

square(-2)

Parameter

Argument

Calling User-Defined Functions

Procedure for calling/applying user-defined functions (for now)

  • Create a new environment frame
  • Bind the function's parameters to its arguments in that frame
  • Execute the body of the function in the new environment

def square(x):

return x * x

square(-2)

Putting it all together

  • Evaluate
    • Evaluate the operator subexpression
    • Evaluate each operand subexpression
  • Apply
    • Apply the value of the operator subexpression to the values of the operand subexpression

def square(x):
return x * x

square(1 - 3)

Operator: square

Function: func square(x)

Operand: 1-3

Argument: -2

Local frame

Formal parameter bound to argument

Names & Environments

  • Every expression is evaluated in the context of an environment.
  • An environment is a sequence of frames
  • So far, there have been two possible environments:
    • The global frame
    • A function's local frame, then the global frame

Rules for looking up names in user-defined functions (version 1)

  • Look it up in the local frame
  • If name isn't in local frame, look it up in the global frame
  • If name isn't in either frame, NameError

Demo

For Thursday

For Thusday

http://pythontutor.com/visualize.html#code=def%20square%28x%29%3A%0A%20%20%20%20return%20x%20*%20x%0A%0Adef%20sum_of_squares%28x,%20y%29%3A%0A%20%20%20%20return%20square%28x%29%20%2B%20square%28y%29%0A%20%20%20%20%0Asum_of_squares%283,%204%29&cumulative=true&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false

Drawing Environment Diagrams

  • Option 1: Python Tutor (tutor.cs61a.org)
    • Useful for quick visualization or for environment diagram questions
  • Option 2: PythonAnywhere (editor.pythonanywhere.com)
    • Includes an integrated editor/interpreter
    • Good for more complicated code or if you want to debug
    • Developed by Rahul Arya, one of your tutors this summer!

Summary

  • Programs consist of statements, or instructions for the computer, containing expressions, which describe computation and evaluate to values.
  • Values can be assigned to names to avoid repeating computations.
  • An assignment statement assigns the value of an expression to a name in the current environment.
  • Functions encapsulate a series of statements that maps arguments to a return value.
  • A def statement creates a function object with certain parameters and a body and binds it to a name in the current environment.
  • A call expression applies the value of its operator, a function, to the value(s) or its operand(s), some arguments.

02 - Names and Functions - Google Slides