ch15 - Chapter 15 Topics in Computer Science Functional...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 15: Topics in Computer Science: Functional Programming 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2
Background image of page 2
Functions: What’s the point? Why do we have functions? More specifically, why have more than one? And if you have more than one, which ones should you have? Once I have functions, what can I use them for? 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Functions are for Managing Complexity Can we write all our programs as one large function? YES, but it gets HARD! As programs grow in size, they grow in complexity. How do you remember lots of details? Put them inside of functions How do you change the function over time and find the right place to make the changes you want? If the function performs a specific role, then if you have to change that role, you change that function. How do you test and debug your program? You can put print statements in the whole thing, or, you can test individual functions first. 4
Background image of page 4
Advantages to using Functions 1. Hides details so that you can ignore them. 2. Makes it clear where you should make changes. If you need to change the title, it’s probably in the title() function. 1. Makes testing easier. 2. Helps you in writing new programs because you can reuse trusted, useful functions. 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Example: Generating a Home Page def makeHomePage(name, interest): file=open("homepage.html","wt") file.write("""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transition//EN" "http://wwww.w3.org/TR/html4/loose.dtd"> <html> <head> <title>"""+name+"""'s Home Page</title> </head> <body> <h1>Welcome to """+name+"""'s Home Page</h1> <p>Hi! I am """+name+""". This is my home page! I am interested in """+interest+"""</p> </body> </html>""") file.close() def makeHomePage(name, interest): file=open("homepage.html","wt") file.write(doctype()) file.write(title(name+"'s Home Page")) file.write(body(""" <h1>Welcome to """+name+"""'s Home Page</h1> <p>Hi! I am """+name+""". This is my home page! I am interested in """+interest+"""</p>""")) file.close() def doctype(): return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transition//EN" "http://wwww.w3.org/TR/html4/loose.dtd">' def title(titlestring): return "<html><head><title>"+titlestring+"</title></hea d>" def body(bodystring): return "<body>"+bodystring+"</body></html>" Which one of these is simpler? 6
Background image of page 6
likely change def makeHomePage(name, interest): file=open("homepage.html","wt") file.write("""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transition//EN" "http://wwww.w3.org/TR/html4/loose.dtd"> <html> <head> <title>"""+name+"""'s Home Page</title> </head> <body> <h1>Welcome to """+name+"""'s Home Page</h1> <p>Hi! I am """+name+""". This is my home page! I am interested in """+interest+"""</p> </body> </html>""") file.close() def makeHomePage(name, interest): file=open("homepage.html","wt") file.write(doctype()) file.write(title(name+"'s Home Page")) file.write(body(""" <h1>Welcome to """+name+"""'s Home Page</h1> <p>Hi! I am """+name+""". This is my home page! I am interested in """+interest+"""</p>"""))
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 87

ch15 - Chapter 15 Topics in Computer Science Functional...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online