300 likes | 314 Views
Learn the importance of defining functions and variables in programming, with examples and guidelines for better code management.
E N D
CSC 160Computer Programmingfor Non-MajorsChapter 3: Programs are Functions Plus Variable Definitions Prof. Adam M. Wittenstein adamwittenstein@adelphi.edu http://www.adelphi.edu/~wittensa/csc160/
What is a program? • In general, a program consists not just of one, but of many definitions. • For example, the UFO animation programs had several definitions: X, Y, ufo-paint, ufo-draw, ufo-erase, tock, and several other predefined ones. • The functions ufo-draw and ufo-erase are called the auxiliary functions to tock. • What is the auxiliary function for ufo-draw? • Answer: ufo-paint
Why do we use auxiliary functions? • If we didn’t first define ufo-draw and ufo-erase, then tock would be written: (define (tock t) (draw (and (draw-solid-disk0 (X t) (Y t) 10 ‘green) (draw-solid-rect0 ( - (X t) 20) (Y t) 40 3 ‘green))) (and (draw-solid-disk0 (X (+ t 1)) (Y (+ t 1)) 10 ‘white) (draw-solid-rect0 (- (X (+ t 1)) 20) (Y (+ t 1)) 40 3 ‘white))) produce (+ t 1)) • And if we didn’t first define X and Y, it would be even worse. • HW6 extra credit: Write tock without defining X and Y. • More extra credit: Write a paragraph describing which way you prefer – defining auxiliary functions like X, Y, ufo-draw, etc. as we did before – or writing one long tock function like this.
What if we made a mistake? • Since using auxiliary functions, we can test out each part, it will be easier to find errors. • If it were all in one function and something went wrong, it would be harder to find out where.
The UFO example • For a UFO animation, we needed to draw stuff (and erase it) and do this at the appropriate locations. • We developed auxiliary functions for locations: X and Y. • We developed auxiliary functions for drawing (and erasing): ufo-draw and ufo-erase. • Then to draw and erase we had another auxiliary function called ufo-paint which put the disk and rectangle together. • Then along with the predefined keywords of define, draw, and produce, we had all the tools necessary, and were able to write tock.
Modifying the UFO animation • Recall the exercise where we modified the UFO: making it yellow and adding an antenna. • Having the several steps of the animation split up into different functions made it easier to find where the appropriate changes needed to be made.
First Law of Programming • Formulate auxiliary function definitions for every dependency between quantities mentioned in the problem statement or discovered with example calculations. • So for the UFO animation some of these dependencies are: --X location on t --Y location on t --Drawing a UFO at t --Erasing a UFO at t
What are variable definitions? • When a number occurs many times in our program(s), we should give it a name using a VARIABLE DEFINITION. • A variable definition associates a name with a value. • One example is 3.14, which we have used in place of PI. Here is how we could give this number a name: (define PI 3.14) • Now, every time we refer to PI, DrScheme replaces it with 3.14.
Why use variable definitions? • Using a name for a constant makes it easier to replace it with a different value. • Suppose our program contains the definition for PI, and we decide that we need a better approximation of PI for the entire program. • By changing the definition to (define PI 3.14159), the improvement is used everywhere where we use PI. If we didn't have a name like PI for 3.14, we would have to find and all instances of 3.14 in the program and replace them with 3.14159. • It is an unwritten rule to always use only capital letters when defining a variable (to distinguish from defining functions).
Second Law of Programming • Give names to frequently used constants and use the names instead of the constants in programs. • Initially, we won't use many variable definitions for constants, because our programs are small. • But, as we learn to write larger programs, we will make more use of variable definitions. • As we will see, the ability to have a single point of control for changes is important for variable and function definitions. • Sample midterm question: I will write out some functions from the animation and ask you to replace the width of 10, with a variable WIDTH. (This would require you define the variable width before writing the functions.) • How would you do this? • Answer: (define WIDTH 10)
Variable Definitions in DrScheme • Main and auxiliary functions can appear in any order in the definitions window. • The only requirement on definition order is that test cases using the function must appear after the main and auxiliary function definitions. • However, the order for variable definitions is significant, because DrScheme evaluates the right-hand side immediately, without looking at the remaining definitions. Therefore,(define RADIUS 5) (define DIAMETER (* 2 RADIUS)) ;works fine, but(define DIAMETER (* 2 RADIUS)) (define RADIUS 5) ;produces an error • ERROR MESSAGE: “reference to undefined identifier: RADIUS''. • The reason is because DrScheme does not yet know the definition of RADIUS when it tries to evaluate (* 2 RADIUS).
CSC 160Computer Programmingfor Non-MajorsChapter 5: Symbolic Information Prof. Adam M. Wittenstein adamwittenstein@adelphi.edu http://www.adelphi.edu/~wittensa/csc160/
Data type: Symbol • The first type of symbolic data. • A symbol is a forward quotation mark followed by a sequence of letters, such as ‘wittenstein and ‘green. • The forward quotation mark is used so that Scheme knows we are writing a symbol and not a variable. • Symbols cannot contain spaces, commas, apostrophes, and certain other special characters. • Symbols were introduced to programming as a way to have the computer reply to users. We could have the computer print an appropriate response to certain messages entered by users.
These are not symbols • ‘hello there (because of the space) • ‘three,four (because of the comma)
Data Type: String • A second type of symbolic data. • Strings are symbols, plus more. • Uses two double quotation marks. examples: “hello”, “Today is Tuesday, October 11th.” • Unlike symbols, numbers, and images, strings are technically a compound data type, because each individual character is its own piece of data. • Since, for the time being, we are only working with the whole string (and not the individual characters), we will ignore this distinction.
Why use strings? • We can use all characters (including spaces and punctuations) in a string. • We have already used a string, “should be”, for our examples when using the Design Recipe to write a program. • There are several predefined functions for strings including string? and string=?, which are similar to the ones for symbols. string? : object -> boolean string=? : string string -> boolean
string? : object -> boolean (string? “small”) -> true (string? “hello”) -> true (string? “three,four”) -> true (string? ‘hello) -> false
string=? : string string -> boolean (string=? “adam” “adam”) ; true (string=? “adam” “joey”) ; false (define Prof “adam”) (string=? Prof “adam”) ; true (string=? Prof “wittenstein”) ; false (string=? Prof “Prof”) ; false (string=? Prof “Adam”) ; false – case sensitive (string=? "Adam" 'Adam) ; ERROR MESSAGE: string=?: expects type <string> as 2nd argument, given: 'Adam; other arguments were: "Adam"
Another Predefined Function string-append : string string … -> string Example: (string-append “Hello” “Joe”) ;”HelloJoe” (string-append “Hello “ “Joe”) ;”Hello Joe” (string-append “Hello” “ Joe”) ;”Hello Joe” (string-append “Hello,” “ Silvia ” “has volunteered.”) ; “Hello, Silvia has volunteered.”
Example: Function that returns a string • Write a function greet. • It says hello to a person.
Example: greet ;Purpose: To say hello to a person. ;Contract : string -> string
Example: greet ;Purpose: To say hello to a person. ;Contract : string -> string “Examples of greet:” (greet “Joe”) “should be” “Hello, Joe”
Example: greet ;Purpose: To say hello to a person. ;Contract : string -> string “Examples of greet:” (greet “Joe”) “should be” “Hello, Joe” (greet “Prof. Wittenstein”) “should be” “Hello, Prof. Wittenstein”
Example: greet ;Purpose: To say hello to a person. ;Contract : string -> string (define (greet name) … name … ) “Examples of greet:” (greet “Joe”) “should be” “Hello, Joe” (greet “Prof. Wittenstein”) “should be” “Hello, Prof. Wittenstein”
Example: greet ;Purpose: To say hello to a person. ;Contract : string -> string (define (greet name) (string-append “Hello, ” name) ) “Examples of greet:” (greet “Joe”) “should be” “Hello, Joe” (greet “Prof. Wittenstein”) “should be” “Hello, Prof. Wittenstein”
Summary… In writing programs, we always: • Split up the job into many smaller functions, instead of using one big function. • Define variables to equal numbers that are used over and over (like PI in geometry, or the width of 10 in our UFO example). We then learned a new data type: strings. • Strings are more powerful than symbols. • Just like numbers, there are predefined functions. • Just like numbers, we can write our own functions.
Next time… • We will work with images. • There are two kinds of images: shapes and pictures. • In the UFO example, we already worked with some shapes. • Like with numbers and strings: --Again, we will see predefined functions. --Again, we will use them to define our own functions.