CS1110 Lecture 6: Function calls Announcements Grades for Lab 1 should all be posted in CMS. Please verify that you have a 1 if you checked off the lab. Let course staff know if your grade is missing! Assignment 1 is out. The first submission is due February 24. CMS will show an extension, but this is only for your corrected revisions later on. No labs next week due to Winter Break. Many office hours are available. See the course website. Slides by D. Gries, L. Lee, S. Marschner, W. White
How Do Functions Work? function call print add_em(3, 4) 2. Assign arguments to arguments def add_em(x, y): return x + y function name add_em x 3 y 4 call frame Return value: 7
How Do Functions Work? function call print add_em(3, 4) arguments def add_em(x, y): 1 sum = x + y 2 return sum 2. Assign arguments to statement numbers function name add_em:1 x 3 y 4 sum local variables 7 call frame 2 program counter Return value: 7
a = 1 b = 2 swap(a, b) 2. Assign arguments to with print, use commas to print several values def swap(x, y): 1 x = y 2 y = x 3 print 'x, y:', x, y Execute this call on paper. What gets printed out? A: x, y: 1 2 B: x, y: 2 1 C: x, y: 2 2 D: x, y: 1 1 E: I don t know
a = 1 b = 2 swap(a, b) arguments module (global) variables a b 1 2 def swap(x, y): 1 x = y 2 y = x 3 print 'x, y:', x, y 2. Assign arguments to swap:1 program counter x y
a = 1 b = 2 swap(a, b) print 'a, b:', a, b arguments module (global) variables a b 1 2 def swap(x, y): 1 t = x 2 x = y 3 y = t 4 print 'x, y:', x, y 2. Assign arguments to Execute this call on paper. What gets printed out? A: x, y: 1 2 B: x, y: 2 1 C: x, y: 2 2 D: x, y: 1 1 E: I don t know
a = 1 b = 2 swap(a, b) print 'a, b:', a, b arguments module (global) variables a b 1 2 def swap(x, y): 1 t = x 2 x = y 3 y = t 4 print 'x, y:', x, y 2. Assign arguments to Execute this call on paper. What gets printed out? A: a, b: 1 2 B: a, b: 2 1 C: a, b: 2 2 D: a, b: 1 1 E: I don t know
a = 1 b = 2 swap(a, b) print 'a, b:', a, b arguments module (global) variables a b 1 2 def swap(x, y): 1 t = x 2 x = y 3 y = t 4 print 'x, y:', x, y 2. Assign arguments to local variables program counter swap:1 x y t
import point p = point.point(1,2,3) q = point.point(3,4,5) swap_x(p, q) print 'p:', p print 'q:', q def swap_x(p, q): 1 t = p.x 2 p.x = q.x 3 q.x = t Execute this code on paper. You will draw 2 objects and a frame. What is in p.x at the end? A: 1 B: 2 C: 3 D: I don t know
import point p = point.point(1,2,3) q = point.point(3,4,5) swap_x(p, q) print 'p:', p print 'q:', q module (global) variables p q id1 id2 def swap_x(p, q): 1 t = p.x 2 p.x = q.x 3 q.x = t id1 Point id2 Point swap_x:1 x 1.0 x 3.0 p q y 2.0 y 4.0 t z 3.0 z 5.0
s lt_speed = 3e8 print g(3) def g(m): 1 E = f(m, lt_speed) 2 return E def f(x, y): 1 return x * (y**2) lt_speed 3 10 8 2. Assign arguments to g:1 m E f:1 x y parameter local variable
How to Draw Things variable object variable name old value value class identifier class name class name attributes methods function name : program counter module name local variables call frame
Online Python Tutor module (global) variables pythontutor.com type in whatever code you want controls for stepping through code output from print goes here frame for call of g frame for call of f