defining functions academic integrity quiz
play

Defining Functions Academic Integrity Quiz Remember : quiz about - PowerPoint PPT Presentation

Lecture 4 Defining Functions Academic Integrity Quiz Remember : quiz about the course AI policy Have posted grades for completed quizes Right now, missing ~130 enrolled students If did not receive at least 9/10, take it again If


  1. Lecture 4 Defining Functions

  2. Academic Integrity Quiz • Remember : quiz about the course AI policy § Have posted grades for completed quizes § Right now, missing ~130 enrolled students § If did not receive at least 9/10, take it again • If you are not aware of the quiz § Go to http://www.cs.cornell.edu/courses/cs11110/ § Click Academic Integrity in side bar § Read and take quiz in CMS 9/4/18 Defining Functions 2

  3. Recall: Modules • Modules provide extra functions, variables § Example : math provides math.cos(), math.pi § Access them with the import command • Python provides a lot of them for us • This Lecture : How to make modules § Atom Editor to make a module Two different programs § Python to use the module 9/4/18 Defining Functions 3

  4. We Write Programs to Do Things • Functions are the key doers Function Call Function Definition • Command to do the function • Defines what function does >>> plus(23) def plus(n): 24 return n+1 >>> • Parameter : variable that is listed within the parentheses of a method header. • Argument : a value to assign to the method parameter when it is called 9/4/18 Defining Functions 4

  5. We Write Programs to Do Things • Functions are the key doers Function Call Function Definition • Command to do the function • Defines what function does >>> plus(23) def plus(n): Function Header 24 return n+1 >>> • Parameter : variable that is listed within the parentheses of a method header. • Argument : a value to assign to the method parameter when it is called 9/4/18 Defining Functions 5

  6. We Write Programs to Do Things • Functions are the key doers Function Call Function Definition • Command to do the function • Defines what function does >>> plus(23) def plus(n): Function Header Function 24 return n+1 Body >>> (indented) • Parameter : variable that is listed within the parentheses of a method header. • Argument : a value to assign to the method parameter when it is called 9/4/18 Defining Functions 6

  7. We Write Programs to Do Things • Functions are the key doers Function Call Function Definition • Command to do the function • Defines what function does >>> plus(23) def plus(n): Function Header Function 24 return n+1 Body >>> argument to declaration of (indented) assign to n parameter n • Parameter : variable that is listed within the parentheses of a method header. • Argument : a value to assign to the method parameter when it is called 9/4/18 Defining Functions 7

  8. Anatomy of a Function Definition name parameters def plus(n): Function Header """Returns the number n+1 Docstring Specification Parameter n: number to add to Precondition: n is a number""" x = n+1 Statements to execute when called return x 9/4/18 Defining Functions 8

  9. Anatomy of a Function Definition name parameters def plus(n): Function Header """Returns the number n+1 Docstring Specification Parameter n: number to add to Precondition: n is a number""" x = n+1 Statements to execute when called return x Use vertical lines when you write Python The vertical line indicates indentation on exams so we can see indentation 9/4/18 Defining Functions 9

  10. The return Statement • Format : return < expression > § Used to evaluate function call (as an expression) § Also stops executing the function! § Any statements after a return are ignored • Example : temperature converter function def to_centigrade(x): """Returns: x converted to centigrade""" return 5*(x-32)/9.0 9/4/18 Defining Functions 10

  11. A More Complex Example Function Definition Function Call def foo(a,b): >>> x = 2 x ? """Return something >>> foo(3,4) Param a: number What is in the box? Param b: number""" x = a y = b return x*y+y 9/4/18 Defining Functions 11

  12. A More Complex Example Function Definition Function Call def foo(a,b): >>> x = 2 x ? """Return something >>> foo(3,4) Param a: number What is in the box? Param b: number""" A: 2 x = a B: 3 y = b C: 16 return x*y+y D: Nothing! E: I do not know 9/4/18 Defining Functions 12

  13. A More Complex Example Function Definition Function Call def foo(a,b): >>> x = 2 x ? """Return something >>> foo(3,4) Param a: number What is in the box? Param b: number""" CORRECT A: 2 x = a B: 3 y = b C: 16 return x*y+y D: Nothing! E: I do not know 9/4/18 Defining Functions 13

  14. Understanding How Functions Work • Function Frame : Representation of function call • A conceptual model of Python 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) 9/4/18 Defining Functions 14

  15. Text (Section 3.10) vs. Class Textbook This Class to_centigrade 1 x –> 50.0 to_centigrade x 50.0 Call : to_centigrade(50.0) Definition : def to_centigrade(x): return 5*(x-32)/9.0 9/4/18 Defining Functions 15

  16. Example: to_centigrade(50.0) 1. Draw a frame for the call Initial call frame 2. Assign the argument value (before exec body) to the parameter (in frame) 3. Execute the function body to_centigrade 1 § Look for variables in the frame § If not there, look for global x 50.0 variables with that name 4. Erase the frame for the call def to_centigrade(x): next line to execute return 5*(x-32)/9.0 1 9/4/18 Defining Functions 16

  17. Example: to_centigrade(50.0) 1. Draw a frame for the call Executing the 2. Assign the argument value return statement to the parameter (in frame) 3. Execute the function body to_centigrade § Look for variables in the frame § If not there, look for global x 50.0 RETURN 10.0 variables with that name 4. Erase the frame for the call def to_centigrade(x): Return statement creates a special variable for result return 5*(x-32)/9.0 1 9/4/18 Defining Functions 17

  18. Example: to_centigrade(50.0) 1. Draw a frame for the call Executing the 2. Assign the argument value return statement to the parameter (in frame) 3. Execute the function body to_centigrade § Look for variables in the frame § If not there, look for global x 50.0 RETURN 10.0 variables with that name 4. Erase the frame for the call def to_centigrade(x): The return terminates; no next line to execute return 5*(x-32)/9.0 1 9/4/18 Defining Functions 18

  19. Example: to_centigrade(50.0) 1. Draw a frame for the call 2. Assign the argument value to the parameter (in frame) E R A 3. Execute the function body S E W § Look for variables in the frame H O § If not there, look for global L E variables with that name F R 4. Erase the frame for the call A M E def to_centigrade(x): But don’t actually return 5*(x-32)/9.0 1 erase on an exam 9/4/18 Defining Functions 19

  20. Call Frames vs. Global Variables The specification is a lie : Global Variables def swap(a,b): a 1 b 2 """Swap global a & b""" tmp = a 1 Call Frame a = b 2 b = tmp swap 3 1 >>> a = 1 a 1 b 2 >>> b = 2 >>> swap(a,b) 9/4/18 Defining Functions 20

  21. Call Frames vs. Global Variables The specification is a lie : Global Variables def swap(a,b): a 1 b 2 """Swap global a & b""" tmp = a 1 Call Frame a = b 2 b = tmp swap 3 2 >>> a = 1 a 1 b 2 >>> b = 2 tmp 1 >>> swap(a,b) 9/4/18 Defining Functions 21

  22. Call Frames vs. Global Variables The specification is a lie : Global Variables def swap(a,b): a 1 b 2 """Swap global a & b""" tmp = a 1 Call Frame a = b 2 b = tmp swap 3 3 x >>> a = 1 a 1 2 b 2 >>> b = 2 tmp 1 >>> swap(a,b) 9/4/18 Defining Functions 22

  23. Call Frames vs. Global Variables The specification is a lie : Global Variables def swap(a,b): a 1 b 2 """Swap global a & b""" tmp = a 1 Call Frame a = b 2 b = tmp swap 3 x x >>> a = 1 a 1 2 b 2 1 >>> b = 2 tmp 1 >>> swap(a,b) 9/4/18 Defining Functions 23

  24. Call Frames vs. Global Variables The specification is a lie : Global Variables def swap(a,b): a 1 b 2 """Swap global a & b""" tmp = a 1 Call Frame a = b 2 b = tmp 3 ERASE THE FRAME >>> a = 1 >>> b = 2 >>> swap(a,b) 9/4/18 Defining Functions 24

  25. Exercise Time Function Definition Function Call def foo(a,b): >>> x = foo(3,4) """Return something Param x: a number What does the Param y: a number""" frame look like x = a at the start ? 1 y = b 2 return x*y+y 3 9/4/18 Defining Functions 25

  26. Which One is Closest to Your Answer? A: B: foo foo 0 1 a 3 b 4 a 3 b 4 C: D: foo foo 1 1 a 3 b 4 a 3 b 4 x y x 3 9/4/18 Defining Functions 26

  27. Which One is Closest to Your Answer? A: B: foo foo 0 1 a 3 b 4 a 3 b 4 E: ¯\_( � )_/¯ C: D: foo foo 1 1 a 3 b 4 a 3 b 4 x y x 3 9/4/18 Defining Functions 27

Recommend


More recommend