Lecture 4 Functions
2 CodeCademy 4.a.
3 CodeCademy: comments. As parameter of a function Second way to introduce variables: What do max, min, abs do on text strings? What if you switch the order of a print and return at the end of a function definition?
4 Careful: 18 refers to (* args), but we met this before Ex. 19: different ways to call functions Ex. 21: watch the order of evaluation ! LPTHW: 18, 19 and 21
6 Recursive functions: def countdown(n): if n > 0: print "I'm counting down, now at %d." % n countdown(n-1) elif n == 0: print "Happy New Year!" countdown(5)countdown(10)
7 And countup: def countup(n): if n > 0: countup(n-1) countup(n-1) print "I'm counting down, now at %d." % n print "I'm counting down, now at %d." % n elif n == 0: print "Happy New Year!" countup(5)countdown(10) Weird !!!
8 And infinitecount: def infinitecount(n): print "I'm still %d !" % n infinitecount(n)infinitecount(10)
9 Turing complete programming languages. Allow to compute _any_ computable function. comparison (==, !=,, =) + Math (+, -, *, /) + If + functions Are Turing complete. Anything that can be done with “for”- or “while”- loops, can also be done with recursion.
10 Assignment for next week: CodeCademy 5.a Lists and Dictionaries (31 min), 5.b A day at the supermarket (28 mins): LPTHW Ex.32 (9 min), Ex. 34 (11 min) Ignore all the long comments on ordinals/cardinals.