Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 160 Computer Programming for Non-Majors Lecture #5b: Designing Programs Prof. Adam M. Wittenstein

Similar presentations


Presentation on theme: "CSC 160 Computer Programming for Non-Majors Lecture #5b: Designing Programs Prof. Adam M. Wittenstein"— Presentation transcript:

1 CSC 160 Computer Programming for Non-Majors Lecture #5b: Designing Programs Prof. Adam M. Wittenstein Wittenstein@adelphi.eduhttp://www.adelphi.edu/~wittensa/csc160/

2 What are function contracts? A contract does three things: ● Specify function name ● Specify how many arguments, and what type(s) ● Specify what type is returned

3 What are function contracts? A contract does three things: ● Specify function name ● Specify how many arguments, and what type(s) ● Specify what type is returned Can’t use any function correctly without knowing its contract!

4 Examples of contracts + takes two or more numbers & returns a number / takes two numbers & returns a number sqrt takes one number & returns a number image-width takes an image & returns a number etc.

5 Shorter contract notation + : number number … -> number / : number number -> number sqrt : number -> number image-width : image -> number etc.

6 REVIEW: Design Recipe 0  Figure out precisely what you need to do.  Tell the computer how to do it.  Check that the computer does it right.

7 Design Recipe – Version 1  Figure out precisely what you need to do. 1. Understand the problem 2. Function contract 3. Write examples (in Scheme notation)  Tell the computer how to do it. 4. Write a skeleton. 5. Fill in the function body.  Check that the computer does it right. 6. Testing and debugging.

8 Why do we need a recipe? ●Programming requires creativity, and sometimes that’s enough. ●But when programs get big and complex, it helps to have a structure to follow. ●Analogous to rules of counterpoint, harmony, etc. in music. Sonata, rondo, virelai, fugue, … ●Helps avoid “blank page syndrome”.

9 Example 1: “cube” Figure out what to do. 1. Understand the assignment yourself. Take any number and multiply it by itself. Then multiply that product by the original number. 2. Write a function contract. ; cube : takes in a number and returns a number 3. Write examples (in Scheme notation). ; (cube 0) “should be” 0 ; (cube 5) “should be” 125 ; (cube -6) “should be” -216

10 Example 1: “cube” Tell the computer how to do it. 4. Write the function skeleton. ; (define (cube num) ; … something involving num … ) 5. Write the function body. (define (cube num) (* num num num))

11 Example 1: “cube” Check if its done right. 6. Testing and debugging. Testing Type in (cube 0). Did you get 0? Type in (cube 5). Did you get 125? Type in (cube -6). Did you get -216? Debugging If the answers to any of these questions is no, look back to find your mistake.

12 Example 2: “greet” Figure out what to do. 1. Understand the assignment yourself. Take any (first) name and say hello to it. 2. Write a function contract. ; greet : word -> sentence 3. Write examples (in Scheme notation). ; (greet ‘adam) “should be” ‘(hello adam) ; (greet ‘Tom) “should be” ‘(hello Tom) ; (greet ‘KATE) “should be” ‘(hello KATE)

13 Example 2: “greet” Tell the computer how to do it. 4. Write the function skeleton. ; (define (greet name) ; … something involving name … ) 5. Write the function body. ; Using the Simply Scheme language mode: (define (greet name) (sentence ‘hello name))

14 Example 2: “greet” Check if its done right. 6. Testing and debugging. Testing Type in (greet ‘adam). Did you get (hello adam)? Type in (greet ‘Tom). Did you get (hello Tom)? Type in (greet ‘KATE). Did you get (hello KATE)? Debugging If the answers to any of these questions is no, look back to find your mistake.

15 Example 3: “greet2” Figure out what to do. 1. Understand the assignment yourself. Take any two names and say hello to it. 2. Write a function contract. ; greet2 : word word -> sentence 3. Write examples (in Scheme notation). ; (greet2 ‘adam ‘witt) “should be” ‘(hello adam witt) ; (greet2 ‘Tom ‘GREEN) “should be” ’(hello Tom GREEN) ; (greet2 ‘KATE ‘Moss) “should be” ‘(hello KATE Moss)

16 Example 3: “greet2” Do it / check its done right. 4. Write the function skeleton. ; (define (greet2 first last) ; … something involving first and last … ) 5. Write the function body. ; Using the Simply Scheme language mode: (define (greet2 first last) (sentence ‘hello first last)) 6. Testing and debugging. ; As always check over your function before ; pressing Run. Then make sure each of your ; examples returns the expected value.

17 In conclusion… The Design Recipe does not replace the need for thinking. It does guide the thinking process. It does have some concrete, automatic steps. Step 5 – Writing the function body – should hopefully be easier because of the first 4 steps. However, thinking and content-area knowledge are also essential, even when using a Design Recipe.

18 Next time… We will define functions involving images (as well as numbers, words, sentences). We will practice using the six-step Design Recipe when writing these functions. Although the Design Recipe make seem pointless now, it is very important when you write longer functions (and programs). Please read the PSP handout before next class.


Download ppt "CSC 160 Computer Programming for Non-Majors Lecture #5b: Designing Programs Prof. Adam M. Wittenstein"

Similar presentations


Ads by Google