Lecture
Zero-Argument Functions
- Yes, functions can take 0 arguments.
- Functions always re-evaluate their bodies when called.
Dice Functions
- In the Hog project, dice are functions that are zero-argument functions.
- IMplement
repeat()
which is a function that returns the # of times in n rolls that an outcome repeat
1
2
3
4
5
6
7
8
9
10
11
12
def repeats(n, dice):
count = 0
prev = None
while n :
outcome = dice()
if outcome == prev:
count += 1
prev = outcome
n -= 1
return count
repeats(20, six_sided)
Higher Order Functions
- Functions are not allowed to change the value of a name associated within another frame. The name will not update in the higher frame.
1
2
3
4
5
6
7
8
9
10
def reprint(n):
def a(word):
k = n
while k:
print(word)
k-=1
return a
twice = reprint(2)
twice('hey')
1
2
hey
hey