http://www.cs.cornell.edu/courses/cs0/208sp Lecture 4: Defining Functions (Ch. 3.4-3.) CS 0 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]
Things to Do Before Next Class Readings: Sections 8., 8.2, 8.4, 8.5, first paragraph of 8.9 Labs: Go to Lab! (Lab 2 is this week) Get Credit for Lab : can be checked off during Tuesday's consulting hours 4:30-9:30 in the ACCEL lab cannot be checked off after 3:45pm Wednesday check online if you received credit: http://www.cs.cornell.edu/courses/cs0/ 208sp/labs/index.php 2
Check out Piazza Post @2 https://piazza.com/class/jckqwmqflaz6i?cid=2 3
4
5
From last time: Function Calls Function expressions have the form fun(x,y, ) Examples (math functions that work in Python): round(2.34) max(a+3,24) function name argument 7
From last time: Modules Modules provide extra functions, variables Access them with the import command Example: module math >>> import math >>> math.cos(2.0) -0.4646836547424 >>> math.pi 3.4592653589793 8
From last time: Modules Module Text # my_module.py """This is a simple module. It shows how modules work""" Interactive Python >>> import my_module >>> my_module.x 9 x = +2 x = 3*x We discussed how to make module variables Have not covered how to make functions 9
simple_math.py >>> import simple_math >>> simple_math.increment() 2 >>> simple_math. increment(2) 3 0
Anatomy of a Function Definition name parameters def increment(n): Function Header """Returns: the value of n+""" return n+ Docstring Specification Statements to execute when called also called Function Body The vertical line indicates indentation Use vertical lines when you write Python on exams so we can see indentation
The return Statement Passes a value from the function to the caller Format: return <expression> Any statements after return are ignored Optional (if absent, special value None will be sent back) 2
Function Calls vs. Definitions Function Call Command to do the function Function Definition Defines what function does >>> simple_math.increment(23) 24 >>> argument to assign to n def increment(n): return n+ declaration of parameter n Parameter: variable that is listed within the parentheses of a function header. Argument: a value to assign to the function parameter when it is called 3
Using simple_math.py Module Text Interactive Python # simple_math.py Python skips >>> import simple_math """module with two simple math functions""" Python skips def increment(n): """Returns: n+""" return n+ Python learns the function definition Python skips everything inside the function Repeat for all functions in module 4
Using simple_math.py Module Text # simple_math.py """module with two simple math functions""" def increment(n): """Returns: n+""" return n+ Interactive Python >>> import simple_math >>> simple_math.increment(23) Now Python executes the function body Python knows what this is! 5
Using simple_math.py Module Text # simple_math.py """module with two simple math functions""" Interactive Python >>> import simple_math >>> simple_math.increment(23) def increment(n): """Returns: n+""" return n+ 6
Using simple_math.py Module Text # simple_math.py """module with two simple math functions""" Interactive Python >>> import simple_math >>> simple_math.increment(23) 24 def increment(n): """Returns: n+""" return n+ 7
Understanding How Functions Work We will draw pictures to show what is in memory Function Frame: Representation of function call Draw parameters as variables (named boxes) Number of statement in the function body to execute next Starts with function name instruction counter parameters local variables (later in lecture) Note: slightly different than in the book (3.9) Please do it this way. 8
Example: get_feet >>> import height >>> height.get_feet(68) def get_feet(height_in_inches): return height_in_inches // INCHES_PER_FOOT 9
Example: get_feet(68) PHASE : Set up call frame next line to execute. Draw a frame for the call 2. Assign the argument value to the parameter (in frame) 3. Indicate next line to execute get_feet height_in_inches 68 def get_feet(height_in_inches): return height_in_inches // INCHES_PER_FOOT 20
Example: get_feet(68) PHASE 2: Execute function body Return statement creates a special variable for result get_feet height_in_inches 68 RETURN 5 def get_feet(height_in_inches): return height_in_inches // INCHES_PER_FOOT 2
Example: get_feet(68) PHASE 2: Execute function body The return terminates; no next line to execute get_feet height_in_inches 68 RETURN 5 def get_feet(height_in_inches): return height_in_inches // INCHES_PER_FOOT 22
Example: get_feet(68) PHASE 3: Erase call frame get_feet height_in_inches 68 RETURN 5 def get_feet(height_in_inches): return height_in_inches // INCHES_PER_FOOT 23
Example: get_feet(68) PHASE 3: Erase call frame But don t actually erase on an exam ERASE WHOLE FRAME def get_feet(height_in_inches): return height_in_inches // INCHES_PER_FOOT 24
Local Variables () Call frames can make local variables >>> import room_numbers >>> room_numbers.lab_rooms() lab_rooms def lab_rooms(): 2 red_room = 235 orange_room = 236 25
Local Variables (2) Call frames can make local variables >>> import room_numbers >>> room_numbers.lab_rooms() lab_rooms 2 def lab_rooms(): red_room 235 2 red_room = 235 orange_room = 236 26
Local Variables (3) Call frames can make local variables >>> import room_numbers >>> room_numbers.lab_rooms() lab_rooms def lab_rooms(): red_room 235 2 red_room = 235 orange_room = 236 orange_room 236 RETURN NONE 27
Local Variables (4) Call frames can make local variables >>> import room_numbers >>> room_numbers.lab_rooms() 2 def lab_rooms(): red_room = 235 orange_room = 236 ERASE WHOLE FRAME Variables are gone! This function is useless. 28
Exercise Time 2 3 Function Definition def foo(a,b): x = a y = b return x*y+y >>> foo(3,4) Function Call What does the frame look like at the start? 29
Which One is Closest to Your Answer? A: B: foo a 3 b 4 foo a 3 b 4 x a C: D: foo a 3 b 4 foo a 3 b 4 x 3 x y 30
Exercise Time 2 3 Function Definition def foo(a,b): x = a y = b return x*y+y >>> foo(3,4) B: Function Call foo a 3 b 4 What is the next step? 3
Which One is Closest to Your Answer? A: B: foo 2 a 3 b 4 foo a 3 b 4 x 3 C: D: foo 2 a 3 b 4 x 3 foo 2 a 3 b 4 x 3 y 32
Exercise Time 2 3 Function Definition def foo(a,b): x = a y = b return x*y+y >>> foo(3,4) C: Function Call foo 2 a 3 b 4 x 3 What is the next step? 33
Exercise Time 2 3 Function Definition def foo(a,b): x = a y = b return x*y+y >>> foo(3,4) Function Call foo 3 a 3 b 4 x 3 y 4 What is the next step? 34
Which One is Closest to Your Answer? A: B: foo 3 foo RETURN 6 a 3 b 4 x 3 y 4 3 RETURN 6 C: D: foo a 3 b 4 x 3 y 4 RETURN 6 ERASE THE FRAME 35
Exercise Time 2 3 Function Definition def foo(a,b): x = a y = b return x*y+y >>> foo(3,4) C: foo Function Call a 3 b 4 x 3 y 4 RETURN 6 What is the next step? 36
Exercise Time 2 3 Function Definition def foo(a,b): x = a y = b return x*y+y >>> foo(3,4) >>> 6 Function Call ERASE THE FRAME 37
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 height.get_feet uses global for height But cannot change values Assignment to a global makes a new local variable! Global Space (for scope_example.py) change_a a 3.5 # scope_example.py """Show how globals work""" a = 4 # global space def change_a(): a a = 3.5 # local variable return a 4 38
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 height.get_feet uses global for height But cannot change values Assignment to a global makes a new local variable! Global Space (for scope_example.py) a 4 get_a # scope_example.py """Show how globals work""" a = 4 # global space def get_a(): return a # returns global 39
Call Frames and Global Variables The specification is a lie: 2 def swap(a,b): """Swap global a & b""" tmp = a a = b b = tmp Global Variables a b 2 Call Frame 3 swap >>> a = >>> b = 2 >>> swap(a,b) a b 2 40
Call Frames and Global Variables The specification is a lie: 2 3 def swap(a,b): """Swap global a & b""" tmp = a a = b b = tmp >>> a = >>> b = 2 >>> swap(a,b) Global Variables a b 2 Call Frame swap 2 a b 2 tmp 4
Call Frames and Global Variables The specification is a lie: 2 3 def swap(a,b): """Swap global a & b""" tmp = a a = b b = tmp >>> a = >>> b = 2 >>> swap(a,b) Global Variables a b 2 Call Frame swap 3 x a 2 b 2 tmp 42
Call Frames and Global Variables The specification is a lie: 2 3 def swap(a,b): """Swap global a & b""" tmp = a a = b b = tmp >>> a = >>> b = 2 >>> swap(a,b) Global Variables a b 2 Call Frame swap x a 2 b 2 tmp x 43
Call Frames and Global Variables The specification is a lie: 2 3 def swap(a,b): """Swap global a & b""" tmp = a a = b b = tmp >>> a = >>> b = 2 >>> swap(a,b) Global Variables a b 2 Call Frame ERASE THE FRAME 44
Call Frames and Global Variables The specification is a lie: 2 3 def swap(a,b): """Swap global a & b""" tmp = a a = b b = tmp >>> a = >>> b = 2 >>> swap(a,b) Global Variables a b 2 Call Frame THIS FUNCTION DOES NOT SWAP ERASE THE FRAME the global a and global b 45
Visualizing Frames: The Python Tutor 46
More Exercises Module Text Interactive Python # my_module.py def foo(x): return x+ >>> import my_module >>> my_module.x What does Python give me? x = +2 x = 3*x A: 9 CORRECT B: 0 C: D: Nothing E: Error 47