0% found this document useful (0 votes)
48 views

Lec4 Functions Recursion

This document discusses programming concepts like decomposition, abstraction, and functions. It introduces functions as a way to write reusable code and achieve decomposition and abstraction. Functions allow breaking problems into separate, self-contained pieces (decomposition) and hiding tedious coding details from the user (abstraction). Key points are that functions take parameters, have a body of code, and return a value. Variable scope is also discussed, where variables are only accessible within their defined scope.

Uploaded by

Torha Sleven
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Lec4 Functions Recursion

This document discusses programming concepts like decomposition, abstraction, and functions. It introduces functions as a way to write reusable code and achieve decomposition and abstraction. Functions allow breaking problems into separate, self-contained pieces (decomposition) and hiding tedious coding details from the user (abstraction). Key points are that functions take parameters, have a body of code, and return a value. Variable scope is also discussed, where variables are only accessible within their defined scope.

Uploaded by

Torha Sleven
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 84

DECOMPOSITION,

ABSTRACTION,
FUNCTIONS

February 22, 2016 6.0001 LECTURE 6 1


HOW DO WE WRITE CODE?
 so far…
• covered language mechanisms
• know how to write different files for each computation
• each file is some piece of code
• each code is a sequence of instructions
 problems with this approach
• easy for small-scale problems
• messy for larger problems
• hard to keep track of details
• how do you know the right info is supplied to the right
part of code
February 22, 2016 6.0001 LECTURE 6 2
GOOD PROGRAMMING
 more code not necessarily a good thing
 measure good programmers by the amount of
functionality
 introduce functions
 mechanism to achieve decomposition and abstraction

February 22, 2016 6.0001 LECTURE 6 3


EXAMPLE -- PROJECTOR
 a projector is a black box
 don’t know how it works
 know the interface: input/output http://www.myprojectorlamps.com/blog/wp-content/
uploads/Dell-1610HD-Projector.jpg

 connect any electronics to it that can communicate


with that input
 black box somehow converts image from input source
to a wall, magnifying it
 ABSTRACTION IDEA: do not need to know how
projector works to use it

February 22, 2016 6.0001 LECTURE 6 4


EXAMPLE -- PROJECTOR
 projecting large image for
Olympics decomposed into
separate tasks for separate
projectors
 each projector takes input
and produces separate output
 all projectors work together
to produce larger image
By Adenosine (Own work) [CC BY-SA 3.0
 DECOMPOSITION IDEA: (http://creativecommons.org/licenses/by-sa/3.0) or
different devices work GFDL (http://www.gnu.org/copyleft/fdl.html)], via
Wikimedia Commons
together to achieve an end
goal
February 22, 2016 6.0001 LECTURE 6 5
APPLY THESE IDEAS TO
PROGRAMMING
 DECOMPOSITION
• Break problem into different, self-contained, pieces
 ABSTRACTION
• Suppress details of method to compute something from
use of that computation

February 22, 2016 6.0001 LECTURE 6 6


CREATE STRUCTURE with
DECOMPOSITION
 in example, separate devices
 in programming, divide code into modules
• are self-contained
• used to break up code
• intended to be reusable
• keep code organized
• keep code coherent
 this lecture, achieve decomposition with functions
 in a few weeks, achieve decomposition with classes

February 22, 2016 6.0001 LECTURE 6 7


SUPPRESS DETAILS with
ABSTRACTION
 in example, no need to know how to build a projector
 in programming, think of a piece of code as a black box
• cannot see details
• do not need to see details
• do not want to see details
• hide tedious coding details
 achieve abstraction with function specifications or
docstrings

February 22, 2016 6.0001 LECTURE 6 8


DECOMPOSITION &
ABSTRACTION
 powerful together
 code can be used many times but only has to be
debugged once!

February 22, 2016 6.0001 LECTURE 6 9


February 22, 2016 6.0001 LECTURE 6 10
FUNCTIONS
 write reusable piece/chunks of code, called functions
 functions are not run in a program until they are
“called” or “invoked” in a program
 function characteristics:
• has a name
• has parameters (0 or more)
• has a docstring (optional but recommended)
• has a body

February 22, 2016 6.0001 LECTURE 6 11


HOW TO WRITE and
CALL/INVOKE A FUNCTION

def is_even( i ):

"""

Input: i, a positive int

Returns True if i is even, otherwise False

"""

print("hi”)

return i%2 == 0

is_even(3)

February 22, 2016 6.0001 LECTURE 6 12


IN THE FUNCTION BODY

def is_even( i ):

"""

Input: i, a positive int

Returns True if i is even, otherwise False

"""

print("hi”)

return i%2 == 0

February 22, 2016 6.0001 LECTURE 6 13


February 22, 2016 6.0001 LECTURE 6 14
VARIABLE SCOPE
 formal parameter gets bound to the value of
actual parameter when function is called
 new scope/frame/environment created when enter a function
 scope is mapping of names to objects

Global scope f scope


def f( x ):
f Some x
x = x + 1 3
code
print('in f(x): x =', x)

return x x 3
x = 3

z = f( x ) call to f, before z
body evaluated
February 22, 2016 6.0001 LECTURE 6 15
VARIABLE SCOPE
 formal parameter gets bound to the value of
actual parameter when function is called
 new scope/frame/environment created when enter a function
 scope is mapping of names to objects

Global scope f scope


def f( x ):
f Some x
x = x + 1 4
code
print('in f(x): x =', x)

return x x 3
x = 3

z = f( x ) z
February 22, 2016 6.0001 LECTURE 6 16
VARIABLE SCOPE
 formal parameter gets bound to the value of
actual parameter when function is called
 new scope/frame/environment created when enter a function
 scope is mapping of names to objects

Global scope f scope


def f( x ):
f Some x
x = x + 1 4
code
print('in f(x): x =', x)

return x x 3
x = 3

z = f( x ) z
February 22, 2016 6.0001 LECTURE 6 17
VARIABLE SCOPE
 formal parameter gets bound to the value of
actual parameter when function is called
 new scope/frame/environment created when enter a function
 scope is mapping of names to objects

Global scope
def f( x ):
f Some
x = x + 1
code
print('in f(x): x =', x)

return x x 3
x = 3

z = f( x ) binding of returned value to 4


variable z z
February 22, 2016 6.0001 LECTURE 6 18
ONE WARNING IF NO
return STATEMENT
def is_even( i ):

"""

Input: i, a positive int

Does not return anything

"""

i%2 == 0

 Python returns the value None, if no return given


 represents the absence of a value

February 22, 2016 6.0001 LECTURE 6 19


return vs. print
 return only has meaning  print can be used outside
inside a function functions
 only one return executed  can execute many print
inside a function statements inside a function
 code inside function but
after return statement not  code inside function can be
executed executed after a print
statement
 has a value associated
with it, given to function  has a value associated with
caller it, outputted to the console

February 22, 2016 6.0001 LECTURE 6 20


FUNCTIONS AS ARGUMENTS
 arguments can take on any type, even functions

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))
February 22, 2016 6.0001 LECTURE 6 21
SCOPE EXAMPLE
 inside a function, can access a variable defined outside
 inside a function, cannot modify a variable defined
outside
def f(y): def g(y): def h(y):
x = 1 print(x) x = x + 1
x += 1 print(x + 1)
print(x) x = 5
x = 5 h(x)
x = 5 g(x) print(x)
f(x) print(x)
print(x)

February 22, 2016 6.0001 LECTURE 6 22


SCOPE EXAMPLE
 inside a function, can access a variable defined outside
 inside a function, cannot modify a variable defined
outside
def f(y): def g(y): def h(y):
x = 1 print x x = x + 1
x += 1
print x x = 5
x = 5 h(2)
x = 5 g(2) print x
f(2) print x
print x

February 22, 2016 6.0001 LECTURE 6 23


HARDER SCOPE EXAMPLE
IMPORTANT
and
TRICKY!

Python Tutor is your best friend to


help sort this out!
http://www.pythontutor.com/
February 22, 2016 6.0001 LECTURE 6 24
SCOPE DETAILS
def g(x): Global scope
def h():
g Some
x = 'abc'
code
x = x + 1

print('in g(x): x =', x)


x 3
h()

return x

z
x = 3

z = g(x)

February 22, 2016 6.0001 LECTURE 6 25


SCOPE DETAILS
def g(x): Global scope g scope
def h():
g Some x
x = 'abc'
3
code
x = x + 1

print('in g(x): x =', x)


x h Some
h() 3
code
return x

z
x = 3

z = g(x)

February 22, 2016 6.0001 LECTURE 6 26


SCOPE DETAILS
def g(x): Global scope g scope
def h():
g Some x
x = 'abc'
43
code
x = x + 1

print('in g(x): x =', x)


x h Some
h() 3
code
return x

z
x = 3

z = g(x)

February 22, 2016 6.0001 LECTURE 6 27


SCOPE DETAILS
def g(x): Global scope g scope h scope
def h():
g Some x x
x = 'abc'
3
4 “abc”
code
x = x + 1

print('in g(x): x =', x)


x h Some
h() 3
code
return x

z
x = 3

z = g(x)

February 22, 2016 6.0001 LECTURE 6 28


SCOPE DETAILS
def g(x): Global scope g scope
def h():
g Some x
x = 'abc'
4
code
x = x + 1

print('in g(x): x =', x)


x h Some
h() 3
code
return x

None
z
x = 3

z = g(x)

February 22, 2016 6.0001 LECTURE 6 29


SCOPE DETAILS
def g(x): Global scope
def h():
g Some
x = 'abc'
code
x = x + 1

print('in g(x): x =', x)


x 3
h()

return x

z 4
x = 3

z = g(x)

February 22, 2016 6.0001 LECTURE 6 30


February 22, 2016 6.0001 LECTURE 6 31
KEYWORD ARGUMENTS AND
DEFAULT VALUES
 Simple function definition, if last argument is TRUE,
then print lastName, firstName; else firstName,
lastName
def printName(firstName, lastName, reverse):
if reverse:
print(lastName + ‘, ‘ + firstName)
else:
print(firstName, lastName)

February 22, 2016 6.0001 LECTURE 6 32


KEYWORD ARGUMENTS AND
DEFAULT VALUES
 Each of these invocations is equivalent
printName(‘Eric’, ‘Grimson’, False)

printName(‘Eric’, ‘Grimson’, reverse = False)

printName(‘Eric’, lastName = ‘Grimson’, reverse = False)

printName(lastName = ‘Grimson’, firstName = ‘Eric’,


reverse = False)

February 22, 2016 6.0001 LECTURE 6 33


KEYWORD ARGUMENTS AND
DEFAULT VALUES
 Can specify that some arguments have default values, so if no
value supplied, just use that value
def printName(firstName, lastName, reverse = False):
if reverse:
print(lastName + ‘, ‘ + firstName)
else:
print(firstName, lastName)

printName(‘Eric’, ‘Grimson’)
printName(‘Eric’, ‘Grimson’, True)

February 22, 2016 6.0001 LECTURE 6 34


February 22, 2016 6.0001 LECTURE 6 35
SPECIFICATIONS
 a contract between the implementer of a function and
the clients who will use it
• Assumptions: conditions that must be met by clients of
the function; typically constraints on values of parameters
• Guarantees: conditions that must be met by function,
providing it has been called in manner consistent with
assumptions

February 22, 2016 6.0001 LECTURE 6 36


def is_even( i ):
"""
Input: i, a positive int
Returns True if i is even, otherwise False
"""
print "hi"
return i%2 == 0

is_even(3)

February 22, 2016 6.0001 LECTURE 6 37


February 22, 2016 6.0001 LECTURE 6 38
WHAT IS RECURSION
 a way to design solutions to problems by divide-and-
conquer or decrease-and-conquer
 a programming technique where a function calls itself
 in programming, goal is to NOT have infinite recursion
• must have 1 or more base cases that are easy to solve
• must solve the same problem on some other input with
the goal of simplifying the larger problem input

February 22, 2016 6.0001 LECTURE 6 39


ITERATIVE ALGORITHMS SO FAR
 looping constructs (while and for loops) lead to
iterative algorithms
 can capture computation in a set of state variables
that update on each iteration through loop

February 22, 2016 6.0001 LECTURE 6 40


MULTIPLICATION –
ITERATIVE SOLUTION
 “multiply a * b” is equivalent to “add a to itself b times”
 capture state by
• an iteration number (i) starts at b
i  i-1 and stop when 0
• a current value of computation (result)
result  result + a

def mult_iter(a, b):


result = 0
while b > 0:
result += a
b -= 1
return result
February 22, 2016 6.0001 LECTURE 6 41
MULTIPLICATION –
RECURSIVE SOLUTION
 recursive step a*b = a + a + a + a + … + a
• think how to reduce
problem to a = a + a + a + a + … + a
simpler/smaller
version of same
problem = a + a * (b-1)

 base case def mult(a, b):


• keep reducing if b == 1:
problem until reach a
simple case that can return a
be solved directly else:
• when b = 1, a*b = a return a + mult(a, b-1)
February 22, 2016 6.0001 LECTURE 6 42
FACTORIAL
n! = n*(n-1)*(n-2)*(n-3)* … * 1

 what n do we know the factorial of?


n=1  if n == 1:
return 1

 how to reduce problem? Rewrite in terms of


something simpler to reach base case
n*(n-1)!  else:
return n*factorial(n-1)

February 22, 2016 6.0001 LECTURE 6 43


RECURSIVE def fact(n):
if n == 1:
FUNCTION return 1
else:
SCOPE return n*fact(n-1)

EXAMPLE print(fact(4))

Global scope fact scope fact scope fact scope fact scope
(call w/ n=4) (call w/ n=3) (call w/ n=2) (call w/ n=1)

fact Some n n n n
4 3 2 1
code

February 22, 2016 6.0001 LECTURE 6 44


SOME OBSERVATIONS

 each recursive call to a function creates its


own scope/environment
 bindings of variables in a scope is not
changed by recursive call
 flow of control passes back to previous
scope once function call returns value

February 22, 2016 6.0001 LECTURE 6 45


ITERATION vs. RECURSION
def factorial_iter(n): def factorial(n):
prod = 1 if n == 1:
for i in range(1,n+1): return 1
prod *= i else:
return prod return n*factorial(n-1)

 recursion may be simpler, more intuitive


 recursion may be efficient from programmer POV
 recursion may not be efficient from computer POV

February 22, 2016 6.0001 LECTURE 6 46


February 22, 2016 6.0001 LECTURE 6 47
INDUCTIVE REASONING
 How do we know that our def mult_iter(a, b):
recursive code will work? result = 0
 mult_iter terminates while b > 0:
because b is initially positive, result += a
and decreases by 1 each time b -= 1
around loop; thus must
eventually become less than 1 return result

 mult called with b = 1 has no


recursive call and stops def mult(a, b):

 mult called with b > 1 makes if b == 1:


a recursive call with a smaller return a
version of b; must eventually
reach call with b = 1 else:
return a + mult(a, b-1)

February 22, 2016 6.0001 LECTURE 6 48


MATHEMATICAL INDUCTION
 To prove a statement indexed on integers is true for all
values of n:
• Prove it is true when n is smallest value (e.g. n = 0 or n = 1)
• Then prove that if it is true for an arbitrary value of n, one
can show that it must be true for n+1

February 22, 2016 6.0001 LECTURE 6 49


EXAMPLE OF INDUCTION
 0 + 1 + 2 + 3 + … + n = (n(n+1))/2
 Proof
• If n = 0, then LHS is 0 and RHS is 0*1/2 = 0, so true
• Assume true for some k, then need to show that
◦ 0 + 1 + 2 + … + k + (k+1) = ((k+1)(k+2))/2
◦ LHS is k(k+1)/2 + (k+1) by assumption that property holds for
problem of size k
◦ This becomes, by algebra, ((k+1)(k+2))/2
• Hence expression holds for all n >= 0

February 22, 2016 6.0001 LECTURE 6 50


RELEVANCE TO CODE?
 Same logic applies
def mult(a, b):
if b == 1:
return a
else:
return a + mult(a, b-1)
 Base case, we can show that mult must return correct answer
 For recursive case, we can assume that mult correctly returns an
answer for problems of size smaller than b, then by the addition step, it
must also return a correct answer for problem of size b
 Thus by induction, code correctly returns answer
February 22, 2016 6.0001 LECTURE 6 51
February 22, 2016 6.0001 LECTURE 6 52
TOWERS OF HANOI
 The story:
• 3 tall spikes
• Stack of 64 different sized discs – start on one spike
• Need to move stack to second spike (at which point
universe ends)
• Can only move one disc at a time, and a larger disc can
never cover up a small disc

By André Karwath aka Aka (Own work) [CC BY-SA 2.5 (http://creativecommons.org/licenses/by-sa/2.5)], via Wikimedia
Commons
February 22, 2016 6.0001 LECTURE 6 53
TOWERS OF HANOI
 Having seen a set of examples of different sized stacks,
how would you write a program to print out the right
set of moves?
 Think recursively!
• Solve a smaller problem
• Solve a basic problem
• Solve a smaller problem

February 22, 2016 6.0001 LECTURE 6 54


def printMove(fr, to):
print('move from ' + str(fr) + ' to ' + str(to))

def Towers(n, fr, to, spare):


if n == 1:
printMove(fr, to)
else:
Towers(n-1, fr, spare, to)
Towers(1, fr, to, spare)
Towers(n-1, spare, to, fr)

February 22, 2016 6.0001 LECTURE 6 55


February 22, 2016 6.0001 LECTURE 6 56
RECURSION WITH MULTIPLE
BASE CASES
 Fibonacci numbers
• Leonardo of Pisa (aka Fibonacci) modeled the following
challenge
◦ Newborn pair of rabbits (one female, one male) are put in a pen
◦ Rabbits mate at age of one month
◦ Rabbits have a one month gestation period
◦ Assume rabbits never die, that female always produces one new
pair (one male, one female) every month from its second month
on.
◦ How many female rabbits are there at the end of one year?

February 22, 2016 6.0001 LECTURE 6 57


Demo courtesy of Prof. Denny Freeman and Adam Hartz
Demo courtesy of Prof. Denny Freeman and Adam Hartz
Demo courtesy of Prof. Denny Freeman and Adam Hartz
Demo courtesy of Prof. Denny Freeman and Adam Hartz
Demo courtesy of Prof. Denny Freeman and Adam Hartz
Demo courtesy of Prof. Denny Freeman and Adam Hartz
Demo courtesy of Prof. Denny Freeman and Adam Hartz
February 22, 2016 6.0001 LECTURE 6 65
Demo courtesy of Prof. Denny Freeman and Adam Hartz
Demo courtesy of Prof. Denny Freeman and Adam Hartz
FIBONACCI
After one month (call it 0) – 1 female Month Females

After second month – still 1 female (now 0 1


pregnant) 1 1
2 2
After third month – two females, one pregnant,
3 3
one not
4 5
In general, females(n) = females(n-1) + 5 8
females(n-2) 6 13
◦ Every female alive at month n-2 will produce one
female in month n;
◦ These can be added those alive in month n-1 to
get total alive in month n

February 22, 2016 6.0001 LECTURE 6 68


FIBONACCI
 Base cases:
• Females(0) = 1
• Females(1) = 1
 Recursive case
• Females(n) = Females(n-1) + Females(n-2)

February 22, 2016 6.0001 LECTURE 6 69


def fib(x):
"""assumes x an int >= 0
returns Fibonacci of x""”
if x == 0 or x == 1:
return 1
else:
return fib(x-1) + fib(x-2)

February 22, 2016 6.0001 LECTURE 6 70


February 22, 2016 6.0001 LECTURE 6 71
RECURSION ON NON-
NUMERICS
 how to check if a string of characters is a palindrome, i.e.,
reads the same forwards and backwards
• “Able was I, ere I saw Elba” – attributed to Napoleon
• “Are we not drawn onward, we few, drawn onward to new era?” –
attributed to Anne Michaels

By Beinecke Library (Flickr: [General Napolean By Larth_Rasnal (Own work) [GFDL


Bonaparte]) [CC BY-SA 2.0 (http://www.gnu.org/copyleft/fdl.html) or CC BY 3.0
(http://creativecommons.org/licenses/by-sa/2.0)], (http://creativecommons.org/licenses/by/3.0)], via
via Wikimedia Commons Wikimedia Commons
February 22, 2016 6.0001 LECTURE 6 72
SOLVING RECURSIVELY?
 First, convert the string to just characters, by stripping
out punctuation, and converting upper case to lower
case
 Then
• Base case: a string of length 0 or 1 is a palindrome
• Recursive case:
◦ If first character matches last character, then is a palindrome if
middle section is a palindrome

February 22, 2016 6.0001 LECTURE 6 73


EXAMPLE
‘Able was I, ere I saw Elba’  ‘ablewasiereisawleba’
isPalindrome(‘ablewasiereisawleba’)
is same as
• ‘a’ == ‘a’ and
isPalindrome(‘blewasiereisawleb’)

February 22, 2016 6.0001 LECTURE 6 74


def isPalindrome(s):
def toChars(s):
s = s.lower()
ans = ''
for c in s:
if c in 'abcdefghijklmnopqrstuvwxyz':
ans = ans + c
return ans
def isPal(s):
if len(s) <= 1:
return True
else:
return s[0] == s[-1] and isPal(s[1:-1])
return isPal(toChars(s))

February 22, 2016 6.0001 LECTURE 6 75


DIVIDE AND CONQUER
 an example of a “divide and conquer” algorithm
 solve a hard problem by breaking it into a set of sub-
problems such that:
• sub-problems are easier to solve than the original
• solutions of the sub-problems can be combined to solve
the original

February 22, 2016 6.0001 LECTURE 6 76


February 22, 2016 6.0001 LECTURE 6 77
MODULES AND FILES
 have assumed that all our code is stored in one file
 cumbersome for large collections of code, or for code
that should be used by many different other pieces of
programming
 a module is a .py file containing a collection Python
definitions and statements

February 22, 2016 6.0001 LECTURE 6 78


EXAMPLE MODULE
 the file circle.py contains
pi = 3.14159
def area(radius):
return pi*(radius**2)
def circumference(radius):
return 2*pi*radius

February 22, 2016 6.0001 LECTURE 6 79


EXAMPLE MODULE
 then we can import and use this module:
import circle
pi = 3
print(pi)
print(circle.pi)
print(circle.area(3))
print(circle.circumference(3))
 results in the following being printed:
3
3.14159
28.27431
18.849539999999998

February 22, 2016 6.0001 LECTURE 6 80


OTHER IMPORTING
 if we don’t want to refer to functions and variables by their
module, and the names don’t collide with other bindings,
then we can use:
from circle import *
print(pi)
print(area(3))
 this has the effect of creating bindings within the current
scope for all objects defined within circle
 statements within a module are executed only the first
time a module is imported

February 22, 2016 6.0001 LECTURE 6 81


FILES
 need a way to save our work for later use
 every operating system has its own way of handling
files; Python provides an operating-system
independent means to access files, using a file handle
nameHandle = open(‘kids’, ‘w’)
 creates a file named kids and returns file handle
which we can name and thus reference. The w
indicates that the file is to opened for writing into.

February 22, 2016 6.0001 LECTURE 6 82


FILES: example
nameHandle = open(‘kids’, ‘w’)
for i in range(2):
name = input(‘Enter name: ‘)
nameHandle.write(name + ‘\’)
nameHandle.close()

February 22, 2016 6.0001 LECTURE 6 83


FILES: example
nameHandle = open(‘kids’, ‘r’)
for line in nameHandle:
print(line)
nameHandle.close()

February 22, 2016 6.0001 LECTURE 6 84

You might also like