The Return Statement a More Complex Example
Total Page:16
File Type:pdf, Size:1020Kb
We Write Programs to Do Things Anatomy of a Function Definition • Functions are the key doers name parameters Function Call Function Definition def plus(n): Function Header """Returns the number n+1 Docstring • Command to do the function • Defines what function does Specification >>> plus(23) Function def plus(n): Parameter n: number to add to 24 Header return n+1 Function Precondition: n is a number""" Body >>> (indented) x = n+1 Statements to execute when called return x • Parameter: variable that is listed within the parentheses of a method header. The vertical line Use vertical lines when you write Python indicates indentation on exams so we can see indentation • Argument: a value to assign to the method parameter when it is called The return Statement A More Complex Example • Format: return <expression> Function Definition Function Call § Used to evaluate function call (as an expression) def foo(a,b): >>> x = 2 § Also stops executing the function! x ? """Return something >>> foo(3,4) § Any statements after a return are ignored Param a: number • Example: temperature converter function What is in the box? Param b: number""" def to_centigrade(x): x = a A: 2 """Returns: x converted to centigrade""" B: 3 y = b C: 16 return 5*(x-32)/9.0 return x*y+y D: Nothing! E: I do not know Understanding How Functions Work Text (Section 3.10) vs. Class • Function Frame: Representation of function call Textbook This Class • A conceptual model of Python to_centigrade 1 Draw parameters • Number of statement in the as variables function body to execute next to_centigrade x –> 50.0 (named boxes) • Starts with 1 x 50.0 function name instruction counter parameters Definition: Call: to_centigrade(50.0) local variables (later in lecture) def to_centigrade(x): return 5*(x-32)/9.0 1 Example: to_centigrade(50.0) Example: to_centigrade(50.0) 1. Draw a frame for the call 1. Draw a frame for the call Initial call frame Executing the 2. Assign the argument value (before exec body) 2. Assign the argument value return statement to the parameter (in frame) to the parameter (in frame) 3. Execute the function body to_centigrade 1 3. Execute the function body to_centigrade § Look for variables in the frame § Look for variables in the frame § If not there, look for global § If not there, look for global variables with that name x 50.0 variables with that name x 50.0 RETURN 10.0 4. Erase the frame for the call 4. Erase the frame for the call def to_centigrade(x): next line to execute def to_centigrade(x): Return statement creates a 1 return 5*(x-32)/9.0 1 return 5*(x-32)/9.0 special variable for result Call Frames vs. Global Variables Function Access to Global Space The specification is a lie: • All function definitions Global Space Global Variables (for globals.py) a 4 def swap(a,b): are in some module a 1 b 2 """Swap global a & b""" • Call can access global get_a 1 1 tmp = a space for that module Call Frame 2 a = b § math.cos: global for math 3 b = tmp swap § temperature.to_centigrade uses global for temperature # globals.py """Show how globals work""" >>> a = 1 a x1 2 b x2 1 • But cannot change values a = 4 # global space >>> b = 2 § Assignment to a global >>> swap(a,b) tmp 1 makes a new local variable! def get_a(): § Why we limit to constants return a # returns global Function Access to Global Space Exercise Time • All function definitions Global Space a 4 Function Definition Function Call are in some module (for globals.py) def foo(a,b): >>> x = foo(3,4) • Call can access global change_a space for that module """Return something § math.cos: global for math a 3.5 Param x: a number What does the § temperature.to_centigrade Param y: a number""" frame look like uses global for temperature # globals.py """Show how globals work""" 1 x = a at the start? • But cannot change values a = 4 # global space 2 y = b § Assignment to a global def change_a(): makes a new local variable! a = 3.5 # local variable 3 return x*y+y § Why we limit to constants return a 2.