lecture 6 - cornell university › ... › presentation-06.pdf · announcements for this lecture...

42
Visualizing Functions Lecture 6

Upload: others

Post on 28-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Visualizing Functions

Lecture 6

Page 2: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Announcements for this Lecture

Last Call

• Quiz: About the Course• Take it by tomorrow• Also remember survey

Assignment 1

• Assignment 1 is live§ Posted on web page§ Due Thur, Sep. 17th

§ Due in place of Lab 4• Lab 3 will help a lot

§ Testing is a major part § Try to finish it first§ But start this Saturday!

9/10/15 Visualizing Functions 2

Page 3: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

One-on-One Sessions

• Still ongoing: 1/2-hour one-on-one sessions§ To help prepare you for the assignment§ Primarily for students with little experience

• There are still some spots available§ Sign up for a slot in CMS

• Will keep running after September 17§ Will open additional slots after the due date§ Will help students revise Assignment 1

9/10/15 Visualizing Functions 3

Page 4: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

A1: The Module urllib2

• Module urllib2 is used to read web pages§ Function urlopen creates a url object§ u = urllib2.urlopen('http://www.cornell.edu')

• url has a method called read()§ Returns contents of web page§ Usage: s = u.read() # s is a string

9/11/14 Objects 4

u

Page 5: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

A Motivating Example

Function Definition

def foo(a,b):"""Do something

Param a: numberParam b: number"""

x = ay = breturn x*y+y

Function Call

>>> x = 2>>> foo(3,4)

9/10/15 Visualizing Functions 5

?x

What is in the box?

Page 6: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

A Motivating Example

Function Definition

def foo(a,b):"""Do something

Param a: numberParam b: number """

x = ay = breturn x*y+y

Function Call

>>> x = 2>>> foo(3,4)

9/10/15 Visualizing Functions 6

?x

What is in the box?

A: 2B: 3C: 16D: Nothing!E: I do not know

Page 7: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

A Motivating Example

Function Definition

def foo(a,b):"""Do something

Param a: numberParam b: number"""

x = ay = breturn x*y+y

Function Call

>>> x = 2>>> foo(3,4)

9/10/15 Visualizing Functions 7

?x

What is in the box?

A: 2B: 3C: 16D: Nothing!E: I do not know

CORRECT

Page 8: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

• Number of statement in thefunction body to execute next

• Starts with 1

Draw parametersas variables (named boxes)

How Do Functions Work?

• Function Frame: Representation of function call• A conceptual model of Python

9/10/15 Visualizing Functions 8

Draw template on a piece of paper

function name

local variables (later in lecture)

parameters

instruction counter

Page 9: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Text (Section 3.10) vs. Class

Textbook This Class

9/10/15 Visualizing Functions 9

def to_centigrade(x):return 5*(x-32)/9.0

Call: to_centigrade(50.0)Definition:

to_centigrade 1

50.0xto_centigrade x –> 50.0

Page 10: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Example: to_centigrade(50.0)

1. Draw a frame for the call2. Assign the argument value

to the parameter (in frame)3. Execute the function body

§ Look for variables in the frame§ If not there, look for global

variables with that name

4. Erase the frame for the call

9/10/15 Visualizing Functions 10

def to_centigrade(x):return 5*(x-32)/9.0

to_centigrade 1

x

Initial call frame(before exec body)

next line to execute1

50.0

Page 11: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Example: to_centigrade(50.0)

1. Draw a frame for the call2. Assign the argument value

to the parameter (in frame)3. Execute the function body

§ Look for variables in the frame§ If not there, look for global

variables with that name

4. Erase the frame for the call

9/10/15 Visualizing Functions 11

def to_centigrade(x):return 5*(x-32)/9.0

to_centigrade

50.0x

Executing thereturn statement

Return statement creates a special variable for result1

RETURN 10.0

Page 12: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Example: to_centigrade(50.0)

1. Draw a frame for the call2. Assign the argument value

to the parameter (in frame)3. Execute the function body

§ Look for variables in the frame§ If not there, look for global

variables with that name

4. Erase the frame for the call

9/10/15 Visualizing Functions 12

def to_centigrade(x):return 5*(x-32)/9.0

to_centigrade

50.0x

Executing thereturn statement

The return terminates;no next line to execute1

RETURN 10.0

Page 13: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Example: to_centigrade(50.0)

1. Draw a frame for the call2. Assign the argument value

to the parameter (in frame)3. Execute the function body

§ Look for variables in the frame§ If not there, look for global

variables with that name

4. Erase the frame for the call

9/10/15 Visualizing Functions 13

def to_centigrade(x):return 5*(x-32)/9.0 But don’t actually

erase on an exam1

Page 14: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Call Frames vs. Global Variables

The specification is a lie:def swap(a,b):

"""Swap global a & b"""tmp = aa = bb = tmp

>>> a = 1>>> b = 2>>> swap(a,b)

9/10/15 Visualizing Functions 14

1a 2b

123 swap 1

1a 2b

Global Variables

Call Frame

Page 15: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Call Frames vs. Global Variables

The specification is a lie:def swap(a,b):

"""Swap global a & b"""tmp = aa = bb = tmp

>>> a = 1>>> b = 2>>> swap(a,b)

9/10/15 Visualizing Functions 15

1a 2b

swap 2

1a 2b

Global Variables

Call Frame

1tmp

123

Page 16: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Call Frames vs. Global Variables

The specification is a lie:def swap(a,b):

"""Swap global a & b"""tmp = aa = bb = tmp

>>> a = 1>>> b = 2>>> swap(a,b)

9/10/15 Visualizing Functions 16

1a 2b

swap 3

1a 2b

Global Variables

Call Frame

1tmp

2

123

x

Page 17: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Call Frames vs. Global Variables

The specification is a lie:def swap(a,b):

"""Swap global a & b"""tmp = aa = bb = tmp

>>> a = 1>>> b = 2>>> swap(a,b)

9/10/15 Visualizing Functions 17

1a 2b

swap

1a 2b

Global Variables

Call Frame

1tmp

2 1

123

x x

Page 18: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Call Frames vs. Global Variables

The specification is a lie:def swap(a,b):

"""Swap global a & b"""tmp = aa = bb = tmp

>>> a = 1>>> b = 2>>> swap(a,b)

9/10/15 Visualizing Functions 18

1a 2b

Global Variables

Call Frame123

Page 19: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Visualizing Frames: The Python Tutor

9/10/15 Visualizing Functions 19

Page 20: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Visualizing Frames: The Python Tutor

9/10/15 Visualizing Functions 20

Global Space

Call Frame

Page 21: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Visualizing Frames: The Python Tutor

9/10/15 Visualizing Functions 21

Global Space

Call Frame

Variables from second lecture

go in here

Page 22: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Visualizing Frames: The Python Tutor

9/10/15 Visualizing Functions 22

Missing line numbers!

Page 23: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Visualizing Frames: The Python Tutor

9/10/15 Visualizing Functions 23

Missing line numbers!

Line numbermarked here

(sort-of)

Page 24: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Global Space (for globals.py)

Function Access to Global Space

• All function definitions are in some module

• Call can access global space for that module§ math.cos: global for math§ temperature.to_centigrade

uses global for temperature• But cannot change values

§ Assignment to a global makes a new local variable!

§ Why we limit to constants9/10/15 Visualizing Functions 24

show_a 1

4a

# globals.py"""Show how globals work"""a = 4 # global space

def show_a():print a # shows global

Page 25: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Global Space (for globals.py)

Function Access to Global Space

• All function definitions are in some module

• Call can access global space for that module§ math.cos: global for math§ temperature.to_centigrade

uses global for temperature• But cannot change values

§ Assignment to a global makes a new local variable!

§ Why we limit to constants9/10/15 Visualizing Functions 25

change_a

3.5a

4a

# globals.py"""Show how globals work"""a = 4 # global space

def change_a():a = 3.5 # local variable

Page 26: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Exercise Time

Function Definition

def foo(a,b):"""Do something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)

9/10/15 Visualizing Functions 26

12

3

What does the frame look like

at the start?

Page 27: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Which One is Closest to Your Answer?

A: B:

8/27/15 Variables & Assignments 27

C: D:foo 1

3a 4b

3x

foo 1

3a 4b

x y

foo 0

3a 4b

foo 1

3a 4b

Page 28: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Which One is Closest to Your Answer?

A: B:

8/27/15 Variables & Assignments 28

C: D:foo 1

3a 4b

3x

foo 1

3a 4b

x y

foo 0

3a 4b

foo 1

3a 4b

E:

¯\_(ツ)_/¯

Page 29: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Exercise Time

Function Definition

def foo(a,b):"""Do something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)B:

9/10/15 Visualizing Functions 29

12

3

foo 1

3a 4b

Page 30: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Exercise Time

Function Definition

def foo(a,b):"""Do something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)B:

9/10/15 Visualizing Functions 30

12

3

foo 1

3a 4b

What is the next step?

Page 31: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Which One is Closest to Your Answer?

A: B:

8/27/15 Variables & Assignments 31

C: D:foo 2

3a 4b

3x

foo 2

3a 4b

3x y

foo 2

3a 4b

foo 1

3a 4b

3x

Page 32: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Exercise Time

Function Definition

def foo(a,b):"""Do something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)C:

9/10/15 Visualizing Functions 32

12

3

foo 2

3a 4b

3x

Page 33: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Exercise Time

Function Definition

def foo(a,b):"""Do something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)C:

9/10/15 Visualizing Functions 33

12

3

foo 2

3a 4b

3x

What is the next step?

Page 34: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Which One is Closest to Your Answer?

A: B:

8/27/15 Variables & Assignments 34

C: D:

foo 3

3a 4b

3x 4y

foo

3a 4b

3x 4y

RETURN

3

foo

3a 4b

3x 4y

RETURN 16

Page 35: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Exercise Time

Function Definition

def foo(a,b):"""Do something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)A:

9/10/15 Visualizing Functions 35

12

3

foo 3

3a 4b

3x 4y

Page 36: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Exercise Time

Function Definition

def foo(a,b):"""Do something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)A:

9/10/15 Visualizing Functions 36

12

3

foo 3

3a 4b

3x 4y

What is the next step?

Page 37: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Which One is Closest to Your Answer?

A: B:

8/27/15 Variables & Assignments 37

C: D:

foo 3 foo

3a 4b

3x 4y

RETURN 16

3

foo

3a 4b

3x 4y

RETURN 16

RETURN 16

Page 38: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Exercise Time

Function Definition

def foo(a,b):"""Do something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)C:

9/10/15 Visualizing Functions 38

12

3

foo

3a 4b

3x 4y

RETURN 16

Page 39: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Exercise Time

Function Definition

def foo(a,b):"""Do something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)C:

9/10/15 Visualizing Functions 39

12

3

foo

3a 4b

3x 4y

RETURN 16

What is the next step?

Page 40: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Which One is Closest to Your Answer?

A: B:

8/27/15 Variables & Assignments 40

C: D:16x

foo

foo

16x

RETURN 16

Page 41: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Exercise Time

Function Definition

def foo(a,b):"""Do something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)D:

9/10/15 Visualizing Functions 41

12

3

16x

Page 42: Lecture 6 - Cornell University › ... › presentation-06.pdf · Announcements for this Lecture Last Call • Quiz: About the Course • Take it by tomorrow • Also remember survey

Exercise Time

Function Definition

def foo(a,b):"""Do something

Param x: a numberParam y: a number"""

x = ay = breturn x*y+y

Function Call

>>> x = foo(3,4)D:

9/10/15 Visualizing Functions 42

12

3

16x

Variable in global space