Lecture 4 Defining Functions
Academic Integrity Quiz • Remember : quiz about the course AI policy § Have posted grades for completed quizes § Right now, missing ~90 enrolled students § If did not receive perfect, 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 8/31/17 Defining Functions 2
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 § Komodo Edit to make a module Two different § Python to use the module programs 8/31/17 Defining Functions 3
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 8/31/17 Defining Functions 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): 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 8/31/17 Defining Functions 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 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 8/31/17 Defining Functions 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 >>> 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 8/31/17 Defining Functions 7
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 8/31/17 Defining Functions 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 Use vertical lines when you write Python The vertical line indicates indentation on exams so we can see indentation 8/31/17 Defining Functions 9
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 8/31/17 Defining Functions 10
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 8/31/17 Defining Functions 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""" A: 2 x = a B: 3 y = b C: 16 return x*y+y D: Nothing! E: I do not know 8/31/17 Defining Functions 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""" CORRECT A: 2 x = a B: 3 y = b C: 16 return x*y+y D: Nothing! E: I do not know 8/31/17 Defining Functions 13
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) 8/31/17 Defining Functions 14
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 8/31/17 Defining Functions 15
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 8/31/17 Defining Functions 16
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 return 5*(x-32)/9.0 special variable for result 1 8/31/17 Defining Functions 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): The return terminates; return 5*(x-32)/9.0 no next line to execute 1 8/31/17 Defining Functions 18
Example: to_centigrade(50.0) 1. Draw a frame for the call 2. 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 def to_centigrade(x): But don’t actually return 5*(x-32)/9.0 1 erase on an exam 8/31/17 Defining Functions 19
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) 8/31/17 Defining Functions 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 2 >>> a = 1 a 1 b 2 >>> b = 2 tmp 1 >>> swap(a,b) 8/31/17 Defining Functions 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 3 >>> a = 1 x a 1 2 b 2 >>> b = 2 tmp 1 >>> swap(a,b) 8/31/17 Defining Functions 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 >>> a = 1 x x a 1 2 b 2 1 >>> b = 2 tmp 1 >>> swap(a,b) 8/31/17 Defining Functions 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 3 >>> a = 1 >>> b = 2 >>> swap(a,b) 8/31/17 Defining Functions 24
Function Access to Global Space • All function definitions Global Space a 4 (for globals.py) are in some module • Call can access global get_a 1 space for that module § math.cos : global for math § temperature.to_centigrade # globals.py uses global for temperature """Show how globals work""" • But cannot change values a = 4 # global space § Assignment to a global makes a new local variable! def get_a(): return a # returns global § Why we limit to constants 8/31/17 Defining Functions 25
Recommend
More recommend