DECOMPOSITION, ABSTRACTION, FUNCTIONS
(download slides and .py files
- follow along!)
6.0001 LECTURE 4
6.0001 LECTURE 4
1
FUNCTIONS (download slides and .py files follow along!) - - PowerPoint PPT Presentation
DECOMPOSITION, ABSTRACTION, FUNCTIONS (download slides and .py files follow along!) 6.0001 LECTURE 4 1 6.0001 LECTURE 4 LAST TIME while loops vs for loops should know how to write both kinds should know when to
(download slides and .py files
6.0001 LECTURE 4
6.0001 LECTURE 4
1
6.0001 LECTURE 4
2
6.0001 LECTURE 4
3
part of code
6.0001 LECTURE 4
4
functionality
6.0001 LECTURE 4
5
with that input
to a wall, magnifying it
projector works to use it
6.0001 LECTURE 4
6
separate tasks for separate projectors
together to achieve an end goal
6.0001 LECTURE 4
7
6.0001 LECTURE 4
8
6.0001 LECTURE 4
9
6.0001 LECTURE 4
10
sufficient, no need to know how to build one
docstrings
“called” or “invoked” in a program
6.0001 LECTURE 4
11
def is_even( i ): """ Input: i, a positive int Returns True if i is even, otherwise False """ print("inside is_even") return i%2 == 0 is_even(3)
6.0001 LECTURE 4
12
def is_even( i ): """ Input: i, a positive int Returns True if i is even, otherwise False """ print("inside is_even") return i%2 == 0
6.0001 LECTURE 4
13
def f( x ): x = x + 1 print('in f(x): x =', x) return x x = 3 z = f( x )
actual parameter when function is called
6.0001 LECTURE 4
14
def f( x ): x = x + 1 print('in f(x): x =', x) return x x = 3 z = f( x )
6.0001 LECTURE 4
15
Global scope f x z Some code f scope x 3 3
6.0001 LECTURE 4
16
Global scope f x z Some code f scope x 4 3
def f( x ): x = x + 1 print('in f(x): x =', x) return x x = 3 z = f( x )
6.0001 LECTURE 4
17
Global scope f x z Some code 3 f scope x 4
def f( x ): x = x + 1 print('in f(x): x =', x) return x x = 3 z = f( x )
returns 4
6.0001 LECTURE 4
18
Global scope f x z Some code 3 4
def f( x ): x = x + 1 print('in f(x): x =', x) return x x = 3 z = f( x )
def is_even( i ): """ Input: i, a positive int Does not return anything """ i%2 == 0
6.0001 LECTURE 4
19
inside a function
inside a function
after return statement not executed
with it, given to function caller
functions
statements inside a function
executed after a print statement
it, outputted to the console
6.0001 LECTURE 4
20
6.0001 LECTURE 4
21
def func_a(): print 'inside func_a' def func_b(y): print 'inside func_b' return y def func_c(z): print 'inside func_c' return z() print func_a() print 5 + func_b(2) print func_c(func_a)
6.0001 LECTURE 4
22
def func_a(): print 'inside func_a' def func_b(y): print 'inside func_b' return y def func_c(z): print 'inside func_c' return z() print func_a() print 5 + func_b(2) print func_c(func_a) Global scope func_a func_b func_c Some code Some code Some code func_a scope returns None None
Global scope func_a func_b func_c
6.0001 LECTURE 4
23
def func_a(): print 'inside func_a' def func_b(y): print 'inside func_b' return y def func_c(z): print 'inside func_c' return z() print func_a() print 5 + func_b(2) print func_c(func_a) Some code Some code Some code func_b scope y 2 returns 2 None 7
Global scope func_a func_b func_c
24
def func_a(): print 'inside func_a' def func_b(y): print 'inside func_b' return y def func_c(z): print 'inside func_c' return z() print func_a() print 5 + func_b(2) print func_c(func_a) Some code Some code Some code func_c scope z func_a func_a scope returns None returns None None 7
6.0001 LECTURE 4
None
6.0001 LECTURE 4
25
def g(y): print(x) print(x + 1) x = 5 g(x) print(x) def h(y): x += 1 x = 5 h(x) print(x) def f(y): x = 1 x += 1 print(x) x = 5 f(x) print(x)
6.0001 LECTURE 4
26
def g(y): print(x) x = 5 g(x) print(x) def h(y): x += 1 x = 5 h(x) print(x) def f(y): x = 1 x += 1 print(x) x = 5 f(x) print(x)
IMPORTANT and TRICKY!
6.0001 LECTURE 4
27
def g(x): def h(): x = 'abc' x = x + 1 print('g: x =', x) h() return x x = 3 z = g(x)
Global scope g x z Some code 3
6.0001 LECTURE 4
28
g scope x h Some code 3
6.0001 LECTURE 4
29
Global scope g x z Some code 3
def g(x): def h(): x = 'abc' x = x + 1 print('g: x =', x) h() return x x = 3 z = g(x)
g scope x h Some code 3 4
6.0001 LECTURE 4
30
Global scope g x z Some code 3
def g(x): def h(): x = 'abc' x = x + 1 print('g: x =', x) h() return x x = 3 z = g(x)
Global scope g x z Some code 3 g scope x h Some code 3 h scope x “abc” 4
6.0001 LECTURE 4
31
def g(x): def h(): x = 'abc' x = x + 1 print('g: x =', x) h() return x x = 3 z = g(x)
returns None
g scope x h Some code None 4
6.0001 LECTURE 4
32
Global scope g x z Some code 3
def g(x): def h(): x = 'abc' x = x + 1 print('g: x =', x) h() return x x = 3 z = g(x)
returns 4
6.0001 LECTURE 4
33
Global scope g x z Some code 3 4
def g(x): def h(): x = 'abc' x = x + 1 print('g: x =', x) h() return x x = 3 z = g(x)
debugged once!
6.0001 LECTURE 4
34
MIT OpenCourseWare https://ocw.mit.edu
6.0001 Introduction to Computer Science and Programming in Python
Fall 2016 For information about citing these materials or our Terms of Use, visit: https://ocw.mit.edu/terms.