180 likes | 196 Views
Learn how to write number functions Scheme-style and expand to other data types like words, sentences, and images. Review ways to cube a number and define functions with examples.
E N D
CSC 160Computer Programmingfor Non-MajorsLecture #5: Defining Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.edu http://www.adelphi.edu/~wittensa/csc160/
This week…. • First, we learn how to write number functions Scheme-style. • Then, we will write functions in just the same way for other data types, like words, sentences, and images.
I. Number Functions with One Argument
REVIEW: Ways to cube a number • (* 5 5 5) “should be” 125 • (* 1234567890 1234567890 1234567890) “should be about 30 digits long” b) (define BIG 1234567890) (* BIG BIG BIG) c) Even better is to define functions…
Example 1: Defining the cube function Idea: Let cube(num) = num * num * num In Scheme… (define (cube num) (* num num num)) (cube 1234567890) (cube (cube 1234567890)) …
But are we sure cube is right?What does “right” mean? • Function must produce correct answers for all legal inputs • Don’t worry about illegal inputs, e.g.(cube “bluebird”) • If even one legal input produces a wrong answer, the function is wrong. • So we need to test every possible legal input. • This is impossible; test a few carefully selected inputs.
But are we sure cube is right?Specifying behavior & test cases • “cube” function takes in a number and returns another number • Example: (cube 0) “should be” 0 • Example: (cube 5) “should be” 125 • Example: (cube -6) “should be” -216 Notice that once you have defined cube, you can use it the same way as any other (predefined) function.
Syntax Rule #3:Defining a Function • (define (func-name param-name[s] ) (expression)) • Example: (define (cube num) (* num num num))
Submitting questions on HW3 • Before defining the cube function: (define (cube num) (* num num num))you need to: --include an example of how the function is called: (cube 5) --along with what its answer "should be": 125.
Submitting questions on HW3 • So, if question 1 were to “define the cube function”, this is what I would expect in the Definitions Window for full credit: ;(cube 5) “should be” 125 (define (cube num) (* num num num))
Example 2: area-of-disk • Recall the mathematical formula: A = Πr2 • We could write this as a Scheme function: (define (area-of-disk r) (* 3.14 (* r r))) • This defines the function area-of-disk.
Example 2: area-of-disk • We can now use (area-of-disk 5) to calculate a disk with a radius of 5: (* 3.14 (* 5 5))
Example 2: area-of-disk • We can now use (area-of-disk 5) to calculate a disk with a radius of 5: (* 3.14 25) = 78.5
When you're Writing a Function… • Function names cannot contain spaces. • You must spell the function name exactly the same way in contract, examples, and definition. • You must spell the parameter name exactly the same way in function header and function body. • You cannot assume that the input will be any specific number (like 2, or 7, or …) • Refer to the input only by the parameter name (e.g. days), so it works on whatever input is actually provided (stuck into the envelope).
Simply Scheme: Exercise 4.5a • Define a Scheme function to convert a temperature from Celsius to Fahrenheit. (Hint: the formula is F = 9/5 * C + 32)
In summary… • We can define our own functions, and call them the same way as predefined functions. • In the function header, we provide a “placeholder” name for the argument(s). • Specific arguments are not given until the function is called. (Another name for “calling a function” is “invoking a function”.)
Coming up… • Functions with multiple parameters • Functions with other data types Homework… • No new reading! • Make sure you have read and understood ALL of Simply Scheme Chapters 2-5 before next class! • Get started on HW3. Consider pairing off with someone.