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/2018sp 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] Things to Do Before


  1. http://www.cs.cornell.edu/courses/cs1110/2018sp 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. Things to Do Before Next Class Readings: § Sections 8.1, 8.2, 8.4, 8.5, first paragraph of 8.9 Labs: • Go to Lab! (Lab 2 is this week) • Get Credit for Lab 1: § 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/cs1110/ 2 2018sp/labs/index.php

  3. Check out Piazza Post @21 https://piazza.com/class/jckqwmqflaz6i?cid=21 3

  4. 4

  5. 5

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

  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.4161468365471424 >>> math.pi 3.141592653589793 8

  8. From last time: Modules Module Text Interactive Python # my_module.py >>> import my_module >>> my_module.x """This is a simple module. 9 It shows how modules work""" • We discussed how to make module variables x = 1+2 • Have not covered how to make functions x = 3*x 9

  9. simple_math.py >>> import simple_math >>> simple_math.increment(1) 2 >>> simple_math. increment(2) 3 10

  10. 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 11

  11. 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) 12

  12. Function Calls vs. Definitions Function Call Function Definition • Command to do the function • Defines what function does >>> simple_math.increment(23) def increment(n): 24 return n+1 >>> argument to declaration of assign to n 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 13

  13. Using simple_math.py Module Text Interactive Python # simple_math.py >>> import simple_math Python skips """module with two simple Python skips math functions""" def increment(n): Python learns the function definition """Returns: n+1""" return n+1 Python skips everything inside the function Repeat for all functions in module 14

  14. Using simple_math.py Module Text Interactive Python # simple_math.py >>> import simple_math >>> simple_math.increment(23) """module with two simple Python knows math functions""" what this is! def increment(n): Now Python executes """Returns: n+1""" the function body return n+1 15

  15. Using simple_math.py Module Text Interactive Python # simple_math.py >>> import simple_math >>> simple_math.increment(23) """module with two simple math functions""" def increment(n): """Returns: n+1""" return n+1 16

  16. Using simple_math.py Module Text Interactive Python # simple_math.py >>> import simple_math >>> simple_math.increment(23) """module with two simple 24 math functions""" def increment(n): """Returns: n+1""" return n+1 17

  17. 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) Note: slightly different than in the book (3.9) Please do it this way. 18

  18. Example: get_feet >>> import height >>> height.get_feet(68) def get_feet(height_in_inches): return height_in_inches // INCHES_PER_FOOT 1 19

  19. 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 height_in_inches 68 def get_feet(height_in_inches): return height_in_inches // INCHES_PER_FOOT 1 20

  20. Example: get_feet(68) PHASE 2: Execute function body Return statement creates a get_feet 1 special variable for result height_in_inches 68 RETURN 5 def get_feet(height_in_inches): return height_in_inches // INCHES_PER_FOOT 1 21

  21. Example: get_feet(68) PHASE 2: The return terminates; no next line to execute Execute function body get_feet height_in_inches 68 RETURN 5 def get_feet(height_in_inches): return height_in_inches // INCHES_PER_FOOT 1 22

  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 1 23

  23. 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(height_in_inches): return height_in_inches // INCHES_PER_FOOT 1 24

  24. Local Variables (1) • Call frames can make “local” variables >>> import room_numbers >>> room_numbers.lab_rooms() lab_rooms 1 def lab_rooms(): red_room = 235 1 orange_room = 236 2 25

  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 red_room = 235 1 orange_room = 236 2 26

  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 red_room = 235 1 orange_room 236 orange_room = 236 2 RETURN NONE 27

  27. Local Variables (4) • Call frames can make “local” variables >>> import room_numbers >>> room_numbers.lab_rooms() E M A R F E L O H W E S def lab_rooms(): A R E red_room = 235 1 orange_room = 236 2 Variables are gone! This function is useless. 28

  28. 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 ? 29

  29. 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 30

  30. 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 ? 31

  31. 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 32

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

  33. 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 ? 34

  34. 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 35

  35. Exercise Time Function Definition Function Call def foo(a,b): >>> foo(3,4) C: 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 ? 36

  36. 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 37

  37. Function Access to Global Space Global Space • All function definitions a 4 are in some module (for scope_example.py) • Call can access global space change_a for that module a § math.cos : global for math 3.5 § height.get_feet uses global for # scope_example.py height """Show how globals work""" • But cannot change values a = 4 # global space § “Assignment to a global” def change_a(): makes a new local variable! a = 3.5 # local variable return a 38

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend