+ All Categories
Home > Documents > CSC 160 Computer Programming for Non-Majors Lecture #5 (continued): More on Writing Functions Prof....

CSC 160 Computer Programming for Non-Majors Lecture #5 (continued): More on Writing Functions Prof....

Date post: 22-Dec-2015
Category:
View: 214 times
Download: 0 times
Share this document with a friend
30
CSC 160 CSC 160 Computer Programming Computer Programming for Non-Majors for Non-Majors Lecture #5 (continued): Lecture #5 (continued): More on Writing Functions More on Writing Functions Prof. Adam M. Wittenstein Prof. Adam M. Wittenstein [email protected] [email protected] http://www.adelphi.edu/~wittensa/csc160/ http://www.adelphi.edu/~wittensa/csc160/
Transcript

CSC 160CSC 160Computer Computer

ProgrammingProgrammingfor Non-Majorsfor Non-Majors

Lecture #5 (continued):Lecture #5 (continued):More on Writing FunctionsMore on Writing Functions

Prof. Adam M. WittensteinProf. Adam M. Wittenstein

[email protected]@adelphi.edu

http://www.adelphi.edu/~wittensa/csc160/http://www.adelphi.edu/~wittensa/csc160/

REVIEW: Names vs. Values

• When we defined cube, we named the argument x.

• But, when we call cube, we give it a specific value for the argument, like 7.

• The x is just a placeholder for whatever argument you want to give the function.

• When you give it a specific argument like 7, that replaces the x.

REVIEW: Function Composition

• Previously, we have used the * function inside the + function, such as in the example (+ 3 (* 4 5)).

• We can do the same when calling user-defined functions, such as (cube (+ 3 2)) or (cube (cube 2)).

II. Word ProblemsII. Word Problems

Why look at word problems?

• Programmers are usually given word problems and not mathematical expressions to solve.

• Sometimes the description is ambiguous and may contain irrelevant information.

• The first job is to extract the important information from the description.

Example 3: wage

• Company XYZ & Co. pays all its employees $12 per hour. Develop a program to determine an employee’s gross pay.

• The last sentence tells us that the goal is to find the gross pay.

• We know that gross = Rate * Hours, or 12*h in this case.

• So the Scheme function is: (define (wage h) (* 12 h))

Special Forms

• When a function (or variable) is defined, nothing is evaluated.

(define X 7)

(define (cube num) (* num num num))

• Evaluation only occurs when you call the variable or function.

(+ X 2)

(cube 5)

III. Number Functions with III. Number Functions with Multiple ArgumentsMultiple Arguments

Example 4: area-of-rect

• Recall the mathematical formula: A = lw.

• We could write this as a Scheme function:

(define (area-of-rect l w) (* l w))

• This defines the function area-of-rect.

Function as generalization

• What’s the average of 17 and 25?--In Scheme notation, (/ (+ 17 25) 2)

• What’s the average of 14 and 68? --In Scheme notation, (/ (+ 14 68) 2)

• What’s the average of a and b?--In Scheme notation, (/ (+ a b) 2)

Function as generalization

• What’s the average of a and b?--In Scheme notation, (/ (+ a b) 2)

• This is the expression, or “body”, of the function.

• The Scheme function is:(define (average a b) ;called “header”

(/ (+ a b) 2)) ;called “body”

Function as generalization

• This can be thought of as abstraction.

• We write a function incorporating the common elements of each of the average problems: 17,25 and 14,68.

• They both involve addition and division by 2.

• In English, we have abstraction also, as we refer to both (17 + 25) / 2 and (14 + 68) / 2 as the same idea, average.

Order of Arguments

• Make sure that when a function has more than one argument, you always put the arguments in the same order.

• For average, this is not as important, because the arguments are simply being added, and the order of addition can be switched.

• In general, though, putting the arguments in the right order is very important.

Testing average

Let’s try out our two average problems:

• (/ ( + 17 25) 2)can now be written as simply:(average 17 25)

• (/ ( + 14 68) 2)can now be written as simply:(average 14 68)

Testing average

All kinds of numbers, including negatives, zero,decimals, and fractions work just the same wayin Scheme:

• (average -4 6) “should be” 1

• (average 0 7) “should be” 3.5

• (average 0.5 1/2) “should be 0.5 or 1/2”

Simply Scheme Exercise 4.9

• Define a Scheme function discount that takes two arguments: an item’s initial price and a percentage discount. It should return the new price.

• Here are some examples:

• (discount 10 5) “should be” 9.5

• (discount 5 10) “should be” 4.5

Simply Scheme Exercise 4.9

• (discount 10 5)

= 10 – (5% of 10)

• (discount 5 10)

= 5 – (10% of 5)

• (discount price off)

= price – (off% of price)

= price – (off / 100 x price)

Simply Scheme Exercise 4.9

= price – (off / 100 x price)= price – ( / off 100) x price= price – (* (/ off 100) price)= (- price (* (/ off 100) price))

(define (discount price off) (- price (* (/ off 100) price)));As always, check that your example comes out

right in the Interactions Window.

IV. Be careful: PitfallsIV. Be careful: Pitfalls

1. One return value only

• For now, all functions have only one return value.

• This is not allowed:(define (sum-of-squares x y)

(square x)(square y))

• In this case, (square x) will be ignored, and your answer will be (square y), which is not what was intended.

1. One return value only

• For now, all functions have only one return value.

• This is not allowed:(define (f x)

(* x 3) ; the result of (* x 3) does (+ x 10)) ; NOT go to (+ x 10)

• What happens?

1. One return value only

• For now, all functions have only one return value.

• This is not allowed:(define (f x)

(* x 3) ; the result of (* x 3) does (+ x 10)) ; NOT go to (+ x 10)

• In this case, (* x 3) will be ignored, and your answer will be (+ x 10), which is not what you intended.

2. Using the same name for function and parameter

• Scheme will get confused and return an error if the following is typed. Why?

(define (square x)

(* x x))

(define (area square)

(square square))

2. Using the same name for function and parameter

• Notice the last line, the square function is called and its argument is square.

(define (square x)

(* x x))

(define (area square)

(square square))

3. Putting an expression instead of a parameter

• In this function,

(define (f (+ 3 x) y)

(* x y))

there is an error. What is it?

3. Putting an expression instead of a parameter

• In this function,

(define (f (+ 3 x) y)

(* x y))

there is an error.

• (define (func-name argument[s]) …

• (define ( f (+ 3 x) y) ….

3. Putting an expression instead of a parameter

• In this function,(define (f (+ 3 x) y)

(* x y))there is an error.

• (define (func-name argument[s]) …• (define ( f (+ 3 x) y) ….• (+ 3 x) CANNOT be an argument

Preparing for next time…Preparing for next time…

In summary…

• Word problems can be translated into Scheme functions.

• The syntax rule for defining functions works regardless of the number of parameters.

• Be careful to follow the rule exactly: watch for spelling, parentheses, etc.

• Defining functions is the same if the arguments and/or return values are other data types. We will see this next class.

CW #2CW #2

1.1. Using the formula Using the formula V = (1/2)lwhV = (1/2)lwh, write a , write a Scheme function that finds the volume of a Scheme function that finds the volume of a triangular prism given its length (l) , width triangular prism given its length (l) , width (w), and height (h). Be sure to write an (w), and height (h). Be sure to write an example in Scheme notation before defining example in Scheme notation before defining the function.the function.

2.2. Without using DrScheme, complete exercise Without using DrScheme, complete exercise 4.4 on page 54 of Simply Scheme. (You can 4.4 on page 54 of Simply Scheme. (You can verify your answer using DrScheme, but be verify your answer using DrScheme, but be sure to explain in full sentences.)sure to explain in full sentences.)

When done, save as “CW2-When done, save as “CW2-01[LastName]Def.scm” and submit using the 01[LastName]Def.scm” and submit using the Digital Dropbox.Digital Dropbox.


Recommended