CSC151 2010S, Class 08: Writing Your Own Procedures Overview: * Some key drawing concepts * Components of an algorithm * What is a procedure? * Why define your own procedures? * How to define your own procedures. * Labs! Admin: * Sam is a bit disorganized today: Class prep time was consumed by "Dad, I forgot to bring my important notebook to school!" * There are no additional readings for Monday. * You should, however, review the readings you did for today. * Quiz 2 distributed. * Due at midnight tonight. * See the quiz policies. * Assignment 3 distributed. * Due next Wednesday. * EC for today's CS table. * EC for tonight's support event for the Booth family. * Continue to use the same lab partners today. * Finish Wednesday's lab (except for prob 5) before starting today's lab. * Expect to finish today's lab on Monday. Key Drawing Concepts * All of the drawing procedures make NEW drawings, and don't affect the original * scaling also affects the position * So it is often best to scale before you shift Components of an Algorithm * Repetition / Loops * Not yet * Named Values (sometimes called variables) * (define name value) * Subroutines/Procedures * TODAY * Conditionals * Not yet * But we do know some ways to get the effects of conditional behavior * Some set of built-in types of values and operations * drawings in the drawings-as-values model e.g., ellipses and rectangles * (drawing-hshift) * define is a kind of operation * numbers * (* num1 num2) * (sqrt __) * colors * (context-set-fgcolor! __) * (context-set_bgcolor! __) * (drawing-recolor DRAWING COLOR) * Values look like "red" "orange" "yellow" * words (strings) * We know that they exist * But we haven't done much with them * images * (image-new __) * (image-load __) * (image-fill! ___) * A way to sequence steps * Put the instructions in order * (define a (+ 2 3)) * (define b (sqrt a)) * (define c (+ b b)) * Via nesting (sqrt (+ 2 3)) * Other issues * We know that in order to make an operation work, we need to put it immediately after an open paren What is a procedure? * An algorithm * A set of instructions * That takes some input (parameters) * May produce some result * May change the underlying value (!) To open! a jodar ; Note: The jodar is the parameter ; Note: Probably changes the state of the jar if the jodar has a screw top then repeatedly turn the top counter-clockwise until it or your arm falls off if the jodar has a pop top then take advantage of the wonder of leverage if nmeither of the previous holds take hammer to jodar Why do we like procedures? * Encapsulate code so that we can refer to it more clearly * Clear * Concise * Parameterized How do we define procedures? * Need to be able to give NAME PARAMETERS - the names we give to the inputs to the procedure, so that we can talk about them INSTRUCTIONS - the steps of the algorithm, that usually work with the parameter (define NAME (lambda (PARAMETERS) INSTRUCTIONS)) (define square (lambda (jadar) (* jadar jadar))) Labs!