Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
Visualizing Functions
Lecture 6
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
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
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
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?
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
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
• 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
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
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
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
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
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
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
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
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
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
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
Visualizing Frames: The Python Tutor
9/10/15 Visualizing Functions 19
Visualizing Frames: The Python Tutor
9/10/15 Visualizing Functions 20
Global Space
Call Frame
Visualizing Frames: The Python Tutor
9/10/15 Visualizing Functions 21
Global Space
Call Frame
Variables from second lecture
go in here
Visualizing Frames: The Python Tutor
9/10/15 Visualizing Functions 22
Missing line numbers!
Visualizing Frames: The Python Tutor
9/10/15 Visualizing Functions 23
Missing line numbers!
Line numbermarked here
(sort-of)
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
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
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?
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
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:
¯\_(ツ)_/¯
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
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?
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
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
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?
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
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
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?
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
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
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?
Which One is Closest to Your Answer?
A: B:
8/27/15 Variables & Assignments 40
C: D:16x
foo
foo
16x
RETURN 16
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
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