lecture 4 defining functions
play

Lecture 4: Defining Functions (Ch. 3.4-3.11) CS 1110 Introduction - PowerPoint PPT Presentation

http://www.cs.cornell.edu/courses/cs1110/2019sp Lecture 4: Defining Functions (Ch. 3.4-3.11) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White] From last time:


  1. http://www.cs.cornell.edu/courses/cs1110/2019sp Lecture 4: Defining Functions (Ch. 3.4-3.11) CS 1110 Introduction to Computing Using Python [E. Andersen, A. Bracy, D. Gries, L. Lee, S. Marschner, C. Van Loan, W. White]

  2. From last time: Function Calls • Function expressions have the form fun (x,y,…) function argument name • Examples (math functions that work in Python): § round(2.34) § max(a+3,24) Let’s define our own functions! 2

  3. Anatomy of a Function Definition name parameters def increment(n): Function Header """Returns: the value of n+1""" Docstring Specification return n+1 Statements to execute when called also called Function Body Use vertical lines when you write Python The vertical line indicates indentation on exams so we can see indentation 3

  4. 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) 4

  5. Function Definitions vs. Calls Function definition def increment(n): return n+1 • Defines what function does • Declaration of parameter n • Parameter: the variable that is listed within the parentheses of a function header. increment(2) Function call • Command to do the function • Argument to assign to n • Argument: a value to assign to the function parameter when it is called simple_math.py 5

  6. Executing the script simple_math.py # simple_math.py Show in python tutor Python skips (put an error inside fn) """script that defines and calls one simple math Python skips function""” def increment(n): Python learns about the function """Returns: n+1""" return n+1 Python skips everything inside the function increment(2) Python executes this statement Now Python executes the function body C:/> python simple_math.py 6

  7. Understanding How Functions Work • We will draw pictures to show what is in memory • Function Frame : Representation of function call Draw parameters • Number of statement in the as variables function body to execute next (named boxes) • Starts with 1 function name instruction counter parameters local variables (later in lecture) 7 Note: slightly different than in the book (3.9) Please do it this way.

  8. Example: get_feet in height.py module >>> import height >>> height.get_feet(68) def get_feet(ht_in_inches): return ht_in_inches // 12 1 8

  9. Example: get_feet(68) PHASE 1: Set up call frame next line to execute 1. Draw a frame for the call 2. Assign the argument value get_feet 1 to the parameter (in frame) 3. Indicate next line to execute ht_in_inches 68 def get_feet(ht_in_inches): return ht_in_inches // 12 1 9

  10. Example: get_feet(68) PHASE 2: Execute function body Return statement creates a get_feet 1 special variable for result ht_in_inches 68 RETURN 5 def get_feet(ht_in_inches): return ht_in_inches // 12 1 10

  11. Example: get_feet(68) PHASE 2: The return terminates; no next line to execute Execute function body get_feet 1 ht_in_inches 68 RETURN 5 def get_feet(ht_in_inches): return ht_in_inches // 12 1 11

  12. Example: get_feet(68) PHASE 3: Erase call frame get_feet ht_in_inches 68 RETURN 5 def get_feet(ht_in_inches): return ht_in_inches // 12 1 12

  13. Example: get_feet(68) PHASE 3: Erase call frame But don’t actually E M A erase on an exam R F E L O H W E S A R E def get_feet(ht_in_inches): return ht_in_inches // 12 1 13

  14. Local Variables (1) • Call frames can make “local” variables >>> import height >>> height.get_feet(68) get_feet 1 def get_feet(ht_in_inches): ht_in_inches 68 feet = ht_in_inches // 12 1 return feet 2 14

  15. Local Variables (2) • Call frames can make “local” variables >>> import height >>> height.get_feet(68) get_feet 1 2 def get_feet(ht_in_inches): ht_in_inches 68 feet = ht_in_inches // 12 1 feet 5 return feet 2 15

  16. Local Variables (3) • Call frames can make “local” variables >>> import height >>> height.get_feet(68) get_feet 1 2 def get_feet(ht_in_inches): ht_in_inches 68 feet = ht_in_inches // 12 1 feet 5 return feet 2 RETURN 5 16

  17. Local Variables (4) • Call frames can make “local” variables >>> import height >>> height.get_feet(68) E M A R F E L O H W E S def get_feet(ht_in_inches): A R E feet = ht_in_inches // 12 1 return feet 2 Variables are gone! This function is over. 17

  18. Exercise Time Function Definition Function Call def foo(a,b): >>> foo(3,4) x = a 1 y = b 2 What does the return x*y+y frame look like 3 at the start ? 18

  19. Which One is Closest to Your Answer? A: B: foo foo 1 1 a 3 b 4 a 3 b 4 x a C: D: foo foo 1 1 a 3 b 4 a 3 b 4 x 3 x y 19

  20. And the answer is… A: B: foo foo 1 1 a 3 b 4 a 3 b 4 � x a C: D: foo foo 1 1 a 3 b 4 a 3 b 4 x 3 x y 20

  21. Exercise Time Function Definition Function Call def foo(a,b): >>> foo(3,4) B: x = a 1 foo y = b 1 2 return x*y+y a 3 b 4 3 What is the next step ? 21

  22. Which One is Closest to Your Answer? A: B: foo foo 2 1 a 3 b 4 a 3 b 4 x 3 C: D: foo foo 2 2 a 3 b 4 a 3 b 4 x 3 x 3 y 22

  23. And the answer is… A: B: foo foo 2 1 a 3 b 4 a 3 b 4 x 3 C: D: foo foo 2 2 a 3 b 4 a 3 b 4 � x 3 x 3 y 23

  24. Exercise Time Function Definition Function Call def foo(a,b): >>> foo(3,4) x = a 1 foo y = b 2 2 return x*y+y a 3 b 4 3 x 3 What is the next step ? 24

  25. Exercise Time Function Definition Function Call def foo(a,b): >>> foo(3,4) x = a 1 foo y = b 3 2 return x*y+y a 3 b 4 3 x 3 y 4 What is the next step ? 25

  26. Which One is Closest to Your Answer? A: B: foo foo 3 3 a 3 b 4 16 RETURN x 3 y 4 16 RETURN C: D: foo E M A R a 3 b 4 F E H T x 3 y 4 E S A R E 16 RETURN 26

  27. And the answer is… A: B: foo foo 3 3 a 3 b 4 16 RETURN x 3 y 4 16 RETURN C: D: foo E M A R a 3 b 4 F E � H T x 3 y 4 E S A R E 16 RETURN 27

  28. Exercise Time Function Definition Function Call def foo(a,b): >>> foo(3,4) x = a 1 foo y = b 2 return x*y+y a 3 b 4 3 x 3 y 4 16 RETURN What is the next step ? 28

  29. Exercise Time Function Definition Function Call def foo(a,b): >>> foo(3,4) >>> 16 x = a 1 y = b 2 E return x*y+y M 3 A R F E H T E S A R E 29

  30. Function Access to Global Space Global Space • Top-most location in INCHES_PER_FT 12 memory called global space • Functions can access get_feet anything in that global space INCHES_PER_FT = 12 get_feet 1 2 … ht_in_inches 68 def get_feet(ht_in_inches): feet 5 feet = ht_in_inches // INCHES_PER_FT 1 return feet 2 get_feet(68) 30

  31. What about this?? Global Space • What if you choose a local INCHES_PER_FT 12 variable inside a function that happens to also be a feet “plural of foot” global variable? get_feet INCHES_PER_FT = 12 get_feet 1 feet = “plural of foot” ht_in_inches 68 … def get_feet(ht_in_inches): feet = ht_in_inches // INCHES_PER_FT 1 return feet 2 get_feet(68) 31

  32. Look, but don’t touch! Can’t change global variables Global Space INCHES_PER_FT 12 “Assignment to a global” makes a new local variable! feet “plural of foot” get_feet INCHES_PER_FT = 12 get_feet 1 2 feet = “plural of foot” ht_in_inches 68 … def get_feet(ht_in_inches): feet 5 feet = ht_in_inches // INCHES_PER_FT 1 return feet 2 get_feet(68) 32

Recommend


More recommend