lecture 4 functions. 2 codecademy 4.a. 3 codecademy: comments. as parameter of a function second...

10
Lecture 4 Functions

Upload: susan-lester

Post on 21-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 4 Functions. 2 CodeCademy 4.a. 3 CodeCademy: comments.  As parameter of a function  Second way to introduce variables:  What do max, min,

Lecture 4

Functions

Page 2: Lecture 4 Functions. 2 CodeCademy 4.a. 3 CodeCademy: comments.  As parameter of a function  Second way to introduce variables:  What do max, min,

2

CodeCademy 4.a.

Page 3: Lecture 4 Functions. 2 CodeCademy 4.a. 3 CodeCademy: comments.  As parameter of a function  Second way to introduce variables:  What do max, min,

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?

Page 4: Lecture 4 Functions. 2 CodeCademy 4.a. 3 CodeCademy: comments.  As parameter of a function  Second way to introduce variables:  What do max, min,

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

Page 5: Lecture 4 Functions. 2 CodeCademy 4.a. 3 CodeCademy: comments.  As parameter of a function  Second way to introduce variables:  What do max, min,

5

Page 6: Lecture 4 Functions. 2 CodeCademy 4.a. 3 CodeCademy: comments.  As parameter of a function  Second way to introduce variables:  What do max, min,

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)

Page 7: Lecture 4 Functions. 2 CodeCademy 4.a. 3 CodeCademy: comments.  As parameter of a function  Second way to introduce variables:  What do max, min,

7

And countup:

def countup(n):if n > 0:

countup(n-1) print "I'm counting down, now at %d." % n

elif n == 0:print "Happy New Year!"

countup(5)countdown(10)

Weird !!!

Page 8: Lecture 4 Functions. 2 CodeCademy 4.a. 3 CodeCademy: comments.  As parameter of a function  Second way to introduce variables:  What do max, min,

8

And infinitecount:

def infinitecount(n):print "I'm still %d !" % ninfinitecount(n)

infinitecount(10)

Page 9: Lecture 4 Functions. 2 CodeCademy 4.a. 3 CodeCademy: comments.  As parameter of a function  Second way to introduce variables:  What do max, min,

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.

Page 10: Lecture 4 Functions. 2 CodeCademy 4.a. 3 CodeCademy: comments.  As parameter of a function  Second way to introduce variables:  What do max, min,

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.