CSC 160 Computer Programming for Non-Majors Lecture #5: Defining Functions Prof. Adam M. Wittenstein

Slides:



Advertisements
Similar presentations
Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
Advertisements

Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
CSC 160 Computer Programming for Non-Majors Lecture #3a: Stepper, Words, Sentences (continued) Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #9: Booleans Prof. Adam M. Wittenstein
Computer Science 1620 Loops.
CSC 160 Computer Programming for Non-Majors Lecture #4: Defining Variables Prof. Adam M. Wittenstein
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
CSC 160 Computer Programming for Non-Majors Lecture #5c: Functions with Images Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #8: Animations I Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Chapter 2: Numbers, Expressions, and Simple Programs Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Chapter 6: Structures Prof. Adam M. Wittenstein
Chapter 6: User-Defined Functions I
CSC 160 Computer Programming for Non-Majors Lecture #11: Conditionals II Prof. Adam M. Wittenstein
Section 2.4: Errors. Common errors ● Mismatched parentheses ● Omitting space after operator or between numbers ● Putting operator between operands.
CSC 160 Computer Programming for Non-Majors Syntax Rules Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #3c: Working with Pictures (continued) Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Draft Chapter: The UFO Example Prof. Adam M. Wittenstein
Section 1.1: Flying a UFO (continued). Conversion recipe 1)Expand abbreviations 2)Parenthesize all subexpressions that have an operator. Should end up.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Introduction Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Chapter 4: Conditional Expressions and Functions Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #5b: Designing Programs Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #7: Variables Revisited Prof. Adam M. Wittenstein
Chapter 6: User-Defined Functions I
CSC 160 Computer Programming for Non-Majors Chapter 3: Programs are Functions Plus Variable Definitions Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #2: What are Functions? Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #3a: Stepper, Words, Sentences Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #5a: Defining Functions on Words and Sentences Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #6: Function Composition Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #3b: Working with Pictures Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #5 (continued): More on Writing Functions Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #12: Syntax and Semantics Prof. Adam M. Wittenstein
Chapter 6: Functions.
19/5/2015CS150 Introduction to Computer Science 1 Announcements  1st Assignment due next Monday, Sep 15, 2003  1st Exam next Friday, Sep 19, 2003  1st.
Please open Daily Quiz 34. A scientific calculator may be used on this quiz. You can keep your yellow formula sheets out when you take the quiz. Remember.
Radian Measure. Many things can be measured using different units.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
1 Functions Lecfture Abstraction abstraction is the process of ignoring minutiae and focusing on the big picture in modern life, we are constantly.
CSC 160 Computer Programming for Non-Majors Chapter 8: Scheme Language Review Prof. Adam M. Wittenstein
1 How to Take Tests 7 Tips for Essay Answers. 2 Test Strategies Remember, as with all essay questions, your answers must be complete sentences with correct.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
COMP 116: Introduction to Scientific Programming Lecture 11: Functions.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Variables and Assignment CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Calling Methods. Review  We can declare and create objects: Dinosaur dino; dino = new Dinosaur();  We can also shortcut the first two lines: Dinosaur.
Python Functions : chapter 3
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Introduction to Computer Science Lab 1: Numbers and Arithmetic.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CS1101: Programming Methodology
Week 9 - Friday.  What did we talk about last time?  Static method practice  Finished the Game of Life.
1 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function Definition float sqrt(float x) { // compute.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
3/17/2016 AIM: How is temperature measured? VOCABULARY  Celsius  Thermometer  Fahrenheit  Eye level.
1 Topic #3: Lambda CSE 413, Autumn 2007 Programming Languages.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
Chapter 1 – Expressions, Equations, and Functions Algebra I A - Meeting 4 Section 1.6 – Represent Functions as Rules and Tables Function – is a pairing.
User-Defined Functions
Higher-Order Procedures
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Chapter 6: User-Defined Functions I
This Lecture Substitution model
CISC101 Reminders Assignment 3 due today.
Presentation transcript:

CSC 160 Computer Programming for Non-Majors Lecture #5: Defining Functions Prof. Adam M. Wittenstein

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 a)(* 5 5 5) “should be” 125 b)(* ) “should be about 30 digits long” b) (define BIG ) (* 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 ) (cube (cube )) …

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)) (define (func-name param-name[s] ) (expression)) Example: Example: (define (cube num) (* num num num))

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 = Πr 2 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: (* ) = 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) Define a Scheme function to convert a temperature from Celsius to Fahrenheit. (Hint: the formula is F = 9/5 * C + 32)

Preparing for Next Time…

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.