Software Design Fall 2004 For next time (lab): 1) read Chapter 4 of "How to think..." 2) prepare for a warmup exercise at the beginning of lab. You should be able to read and write functions that take parameters (but don't worry about return values yet) For Monday: 1) read Chapter 5 of "How to think..." Olinux Chapter 2 ---------------- If you are not completely sick of hearing me spout off about Free Software, check out my way-too-long article from last year's Frankly Speaking: http://projects.olin.edu/newspaper/article.php?id=92 New tools 1) emacs 2) cat and more 3) grep and sed 4) mkdir and rmdir 5) variables and aliases in .bashrc 6) rm and rm -i function definitions -------------------- function def is an example of a compound statement 1) header ending in : 2) indented body By convention, indentation is 4 spaces. End of body marked by an outdented line. def twice(s): print s, s def fourtimes(s): twice(s) twice(s) A function definition doesn't DO anything except: 1) create a function object 2) create a name that refers to the object function calls -------------- To execute the body of the function, you have to call/invoke it 1) built-in functions len('allen') 2) functions from a module import math print math.sin(math.pi/4) - math.sqrt(2)/2 3) user defined functions fourtimes('allen') arguments are expressions: s = 'downey' fourtimes('allen' + ' ' + s) make some errors ---------------- Try out the following and see what goes wrong: 1) leave off the colon : def fun() x = 1 2) mess up the indentation def fun(): x = 1 y = 1 def fun(): x = 1 y = 1 3) call a function with the wrong number of arguments len('allen', 'downey') 4) call something that is not a function x = 5 x(17) function names are variables ---------------------------- Try this out, and see if you can make sense of it. You might want to draw a state diagram. def is_divisible(x, y): if x%y == 0: print 'yes!' print is_divisible f = is_divisible f(100,10) is_divisible = 'banana' print f print is_divisible functions as parameters (early bird special) ----------------------- Try this out, and see if you can make sense of it. def do_twice(f, s): f(s) f(s) def print_twice(s): print s, s do_twice(print_twice, 'allen') Hint: see www.volleyball.org/people/allen_allen.html The following is for lab tomorrow... Printing -------- 1) go to the printer you want to use and copy the name and IP address from the sticker For example, OC371R03 is 10.24.11.229 2) As root, type printconf & A printer configuration window should appear. 3) Press New. A configuration wizard should appear. 4) Press Forward to get to the first useful screen, then enter the name you want to use to refer to this printer. Don't bother with the description. Press Forward. 5) Select queue type Networked UNIX (LPD). In the server entry, type the IP address. In the queue entry, type the name of the printer again (or whatever, it doesn't matter). Press Forward. 6) Under Printer Model, select Postscript Printer from the scrollbox. Press Forward. 7) Press finish. 8) When it asks, don't print a test page. We'll use your homework as a test page. a2ps ---- a2ps stands for "ASCII to Postscript". It is a versatile program for printing plain text files. 0) Before you try to print something, make sure your network is working. That way we can separate printer problems from network problems. Since you know the IP address of a printer, try to ping it: ping 10.24.11.229 1) Log into your user account and go to the directory where your homework solution is. 2) Check that there is a comment in your program that has your name in it. 3) Type a2ps -1 hello.py (that's a one, not an ell) The -1 option means "one page per sheet of paper". The default, unfortunately, is "two pages per sheet", which is too small for my aging, myopic eyes.