1 / 18

Defining Functions in Computer Programming for Non-Majors

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.

tammyo
Download Presentation

Defining Functions in Computer Programming for Non-Majors

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 160Computer Programmingfor Non-MajorsLecture #5: Defining Functions Prof. Adam M. Wittenstein Wittenstein@adelphi.edu http://www.adelphi.edu/~wittensa/csc160/

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

  3. I. Number Functions with One Argument

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

  5. 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)) …

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

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

  8. Syntax Rule #3:Defining a Function • (define (func-name param-name[s] ) (expression)) • Example: (define (cube num) (* num num num))

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

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

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

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

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

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

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

  16. Preparing for Next Time…

  17. 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”.)

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

More Related