CSC153.01 2004S, Class 4: User-Defined Procedures Admin: * Questions on the homework? (Due Wed.) * Reminder: Work as a team. * SGA senator applications due today at 5:00 p.m. * Fun stuff on Thursday; Earn extra credit Overview: * Why to define your own procedures. * How to define your own procedures. * Lab * Reflection What is a procedure? * It's a section of a code that you can perform more than once. * It is *parameterized*, so that you can perform it on different values. Why do we bother defining procedures? * More efficient for programmers; You don't have to write the code twice. * Allows some sharing: You can use someone else's work * Easier to read code: Well-named procedures help give you a macroscopic picture * Our faculty or bosses require us to do so. * Helps you organize your thoughts; Segment the big problem into smaller pieces * Easier to correct code. In Scheme, to define a procedure (define PROCNAME (lambda (PARAM1 PARAM2 ... PARAMN) EXPRESSION)) To call a procedures (PROCNAME EXP1 EXP2 ... EXPN) Scheme (1) Evaluates each expression (2) Associates the value with the corresponding parameter name (3) Evaluates the 'body' of the procedure, using those names (4) Forgets about all the names (5) Returns the value computed in step 3. Alternate definition (define (PROCNAME PARAM1 PARAM2 ... PARAMN) EXPRESSION) Begin the lab Reflections, Questions, and Such * Notes on types * Built-in procedures check their types * Yours do not, unless you make them do so * There are times in which you will want to write procedures that accept multiple types (cons is such a procedure; it will accept *any* type as its first parameter) * Some types are not checkable (my parameter must be a function from integers to strings) * The Scheme designers had some interesting ideas of fun. Hence, if you find (car (cdr x)) wordy, you can instead use (cadr x) * Yes, there are dozens of variants. (caddr, cdadr, etc.) * No, most people do not attempt to pronounce these variants * As you may have noted, Scheme lets you use some strange characters in your names. * Given that you don't yet know how to create "local variables" within procedures, is there a way to compute both roots of a quadratic without redoing the "square root of b squared - 4 a c" computation?