CSC 160 Computer Programming for Non-Majors Lecture #6: Function Composition Prof. Adam M. Wittenstein

Slides:



Advertisements
Similar presentations
Higher-Order Functions and Loops c. Kathi Fisler,
Advertisements

Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
Lists Introduction to Computing Science and Programming I.
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
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 Images: Another Form of Symbolic Data Prof. Adam M. Wittenstein
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 Lecture #5: Defining Functions 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
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.
Section 2.5: Designing Programs. REVIEW: Design Recipe  Figure out precisely what you need to do.  Tell the computer how to do it.  Check that the.
CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Section 1.2: Drawing a UFO 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
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
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
Programming with Images (continued). Another teachpack – “tiles.ss” Download from the website: “ Save.
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
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
DAT602 Database Application Development Lecture 15 Java Server Pages Part 1.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
CSC 160 Computer Programming for Non-Majors Chapter 8: Scheme Language Review Prof. Adam M. Wittenstein
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Introduction to Computer Programming
1 PL\SQL Dev Templates. 2 TEMPLATE DEFINITION Whenever you create a new program unit, its initial contents are based upon a template which contains pre-defined.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
3 DERIVATIVES.  Remember, they are valid only when x is measured in radians.  For more details see Chapter 3, Section 4 or the PowerPoint file Chapter3_Sec4.ppt.
CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
JavaScript 101 Lesson 6: Introduction to Functions.
1 Topic #3: Lambda CSE 413, Autumn 2007 Programming Languages.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 10: Files.
Subprograms Functions Procedures.
User-Written Functions
FIGURE 4-10 Function Return Statements
8.4 Volume and Areas of Similar Figures
User-Defined Functions
O.S Lecture 13 Virtual Memory.
FIGURE 4-10 Function Return Statements
Functions.
FIGURE 4-10 Function Return Statements
Linear Functions of a Discrete Random Variable
Lecture 3: Rules of Evaluation CS200: Computer Science
FIGURE 4-10 Function Return Statements
Agenda for Unit 3: Functions
Presentation transcript:

CSC 160 Computer Programming for Non-Majors Lecture #6: Function Composition Prof. Adam M. Wittenstein

A preview… A Scheme program includes one or more functions, defined using the rule: (define (function-name parameter-names) (expression)) A Scheme program may also includes one or more variables, defined using the rule: (define VARIABLE-NAME its-value)

REVIEW: Meaning of the Function Definition In the function: (define (square r) (* r r)) we tell Scheme that: (square r) is the same thing as typing: (* r r)

Example 1: area-of-disk Suppose we want to write a function to find the area of a circle. The formula is A = PI * r 2 We could write: (define (area-of-disk r) (* PI (* r r))) However, notice that (* r r) is actually just the function body of the square function.

Example 1: area-of-disk So we can instead write: (define (area-of-disk r) (* PI (square r))) replacing (* r r) with (square r). Even if we named our parameter something different in the square function, it is still allowed.

Example 1: area-of-disk Then, we have: (define (square num) (* num num)) (define (area-of-disk r) (* PI (square r)) Since our goal here is to write area-of- disk, we call that the main function. Then, square which helps us write area-of-disk, is called an auxiliary function.

Example 2: checkerboard The checkerboard function can be written as: (define (checkerboard color1 color2) (image-above (image-beside (rectangle ‘solid color1) (rectangle ‘solid color2) ) (image-beside (rectangle ‘solid color2) (rectangle ‘solid color1) )

Example 2: checkerboard The program can more succinctly (and clearly) be written using function composition: Since we are using the following setup, (image-above (image-beside ____ _____) ) we can write a separate function for that part.

Example 2: checkerboard ;Auxiliary function: (define (counterchange img1 img2) (image-above (image-beside img1 img2) (image-beside img2 img1) )

Example 2: checkerboard ; This allows the checkerboard function to be ; written in this shorter way. ;Main function: (define (checkerboard color1 color2) (counterchange (rectangle ‘solid color1) (rectangle ‘solid color2) )

Writing Multi-Function Programs While developing a Scheme function, you may realize the need or benefit of an auxiliary function. One example is when you have a setup of several functions like image-above and image- beside. You can create a separate function that incorporates those three. We called that separate function counterchange. Another example is when you have the same or similar code twice in a single function. Then you can write an auxiliary function that addresses just the part of the function with that same or similar code.

Writing Multi-Function Programs Sometimes the auxiliary function is predefined, like + or first. If the auxiliary function is not predefined, you will have to define it yourself. You should pause the designing of your main function, while you complete a separate Design Recipe for the auxiliary function(s). Then complete the Design Recipe for the main function.

CW3: For Section 02 only (the 4:30 class) Develop a Scheme function called add-question-mark that takes in a sentence and returns the same sentence with a question mark at the end of the last word. Develop a Scheme function called add-question-mark that takes in a sentence and returns the same sentence with a question mark at the end of the last word. Submit the Definitions Window ONLY using the Digital Dropbox on Blackboard with the file name: “CW3-02[LastName]Def.scm” Submit the Definitions Window ONLY using the Digital Dropbox on Blackboard with the file name: “CW3-02[LastName]Def.scm”

Exercise 5.21: query Develop a Scheme function, query, that turns a statement into a question by swapping the first two words and adding a question mark to the last word. Develop a Scheme function, query, that turns a statement into a question by swapping the first two words and adding a question mark to the last word. Example: Example: ; (query ‘(you are experienced)) “should be” ; (ARE YOU EXPERIENCED?)

Preparing for Next Time…

In summary… When there are multiple things that need to happen in a program, write them as separate functions. When the auxiliary function you want to use is not predefined, define your own. Whenever developing a function that requires auxiliary functions, complete the steps of the Design Recipe separately for every function you write.

Next time… More on variables If you haven’t already, please read the following before next class: –Simply Scheme, pages –How to Design Programs, section 3.