Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 160 Computer Programming for Non-Majors Chapter 3: Programs are Functions Plus Variable Definitions Prof. Adam M. Wittenstein

Similar presentations


Presentation on theme: "CSC 160 Computer Programming for Non-Majors Chapter 3: Programs are Functions Plus Variable Definitions Prof. Adam M. Wittenstein"— Presentation transcript:

1 CSC 160 Computer Programming for Non-Majors Chapter 3: Programs are Functions Plus Variable Definitions Prof. Adam M. Wittenstein adamwittenstein@adelphi.eduhttp://www.adelphi.edu/~wittensa/csc160/

2 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

3 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.

4 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.

5 Section 3.1: Composing Functions

6 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.

7 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.

8 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

9 Section 3.2: Variable Definitions

10 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.

11 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).

12 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)

13 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).

14 CSC 160 Computer Programming for Non-Majors Chapter 5: Symbolic Information Prof. Adam M. Wittenstein adamwittenstein@adelphi.eduhttp://www.adelphi.edu/~wittensa/csc160/

15 Actions, things, placeholders, types, expressions English languageProgramming language Verb (action) go, eat, buy, give Function +, sqrt, cube, under-21? Proper noun (specific thing) Adelphi, Adam, Rover Literal 3, “hello there” Pronoun (thing in context) him, her, it Variable x, num, age Improper noun (kind of thing) college, professor, dog, picture Data type number, symbol, string, image Noun phrase (specific thing, not by name) Adam’s mother Expression (+ 3 (* 4 5)), (cube 7)

16 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.

17 These are not symbols ‘hello there (because of the space) ‘three,four (because of the comma)

18 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.

19 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

20 string? : object -> boolean (string? “small”) -> true (string? “hello”) -> true (string? “three,four”) -> true (string? ‘hello) -> false

21 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 as 2nd argument, given: 'Adam; other arguments were: "Adam"

22 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.”

23 Example: Function that returns a string Write a function greet. It says hello to a person.

24 Example: greet ;Purpose: To say hello to a person. ;Contract : string -> string

25 Example: greet ;Purpose: To say hello to a person. ;Contract : string -> string “Examples of greet:” (greet “Joe”) “should be” “Hello, Joe”

26 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”

27 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”

28 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”

29 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.

30 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.


Download ppt "CSC 160 Computer Programming for Non-Majors Chapter 3: Programs are Functions Plus Variable Definitions Prof. Adam M. Wittenstein"

Similar presentations


Ads by Google