Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Assignment 6.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Spring Semester 2013 Lecture 5
 Communication:  Provide context, e.g., s …  TA Office hour for next Tuesday: CANCELLED  My name is: ??  Show  Scores on-line, Quiz-1 results.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Changing Control.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Sep 26, Fall 2006IAT 800 Lecture 6 Methods and Classes.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Introduction to Methods
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
Computation as an Expressive Medium Lab 3: (Methods and) Classes Jason Alderman.
Programming in Processing Taught by Ms. Madsen Assistants: Ms. Fischer and Ms. Yen Winsor School, 2/6/08.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004.
CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 We’re underway …
1 CS161 Introduction to Computer Science Topic #9.
Elementary C++. Procedural Programming Split your problem into simpler parts then solve each part separately Recognize common parts and solve them only.
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Working With Objects Tonga Institute of Higher Education.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 More details and explanation …
 Functions package up computation … when do we use them? All the time.  Write some simple code to achieve a goal … 12/20/2015© 2010 Larry Snyder, CSE1.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 3: User-Defined Functions I
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Functional Abstraction Reduces Complexity.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004.
Week 2 - Wednesday CS 121.
User-Written Functions
Chapter 6: User-Defined Functions I
Functions in Processing CSE 120 Spring 2017
Functions CIS 40 – Introduction to Programming in Python
Layering: Building Functions out of Functions
Learning to program with Logo
Object Oriented Programming (OOP) LAB # 5
Announcements Mid-Term Exam!! Quiz 5 Again + Quiz 6 Exercise 5
Testing and Repetition
Chapter 6: User-Defined Functions I
Topics Introduction to Functions Defining and Calling a Function
Two Paths Diverge in the Lectures
Variables, Assignments Testing + Iteration
Announcements How to save your work? Quiz solution 5/5/2019
IAT 800 Foundations of Computational Art and Design
Announcements: Questions: Names on the board? Why?
Lawrence Snyder University of Washington
Announcements Survey feedback: summary Quiz: HW 7:
CPS125.
Corresponds with Chapter 5
CMPT 120 Lecture 15 – Unit 3 – Graphics and Animation
Top-Down Design with Functions
Methods and Data Passing
Today: to list: Syllabus change: Grade on-line
Parameters and Arguments
Presentation transcript:

Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Assignment 6

 We will review some of the datatype and variable information from last lecture, but mostly we will …  Learn about functions in Processing … Recall Our Love Affair With Functions  Lightbot 2.0 we were introduced: “beautiful!”  HW3: F.turn( ) …  Lab4: Columns 6/2/2016© 2010 Larry Snyder, CSE2 I’ve said …

 Formulating blocks of computation as a “concept” is functional abstraction  What we did [in Lec 3] is important …  We spotted a coherent (to us) part of the task  We solved it using a sequence of instructions  We put the solution into a function “package”, gave it a name, “process a riser,” and thus created a new thing, a concept, something we can talk about & use  Then we used it to solve something more complicated … and then we did it again! 6/2/2016© Larry Snyder3

 Collecting operations together and giving them a name is key to functional abstraction  The operations perform a coherent activity or action – they become a concept in our thinking  The operations accomplish a goal that is useful – and typically – is needed over and over again  Functions implement functional abstraction: 3 parts ▪ A name ▪ A definition (instruction seq), frequently called a “body” ▪ Parameters –stuff inside the parentheses, covered later 6/2/2016© Larry Snyder4

 So, that’s the idea … how does it look in Processing?  Recall these components of the function declaration 6/2/2016© 2010 Larry Snyder, CSE5 ( ) { } Function’s return type – Use ‘void’ if none Name starts will letter, uses letters, numbers underscore; doesn’t collide The body implements the function … normal Processing statements Required; contain parameters if any Required; contain function definition

 Parameters are the data that goes inside the parentheses in a function  For example  Notice:  The datatype of the parameter must be given  Parameters are separated by commas  Parameter names like other names – no conflicts 6/2/2016© 2010 Larry Snyder, CSE6 void white5by5Box( int xPos, int yPos) { fill(255,255,255); rect(xPos, yPos, 5, 5); }

 Parameters give the data for the function to operate on … then to do the same operation on different data just change the parameters void tile(int xPos, int yPos, color tinto) { fill( tinto ); rect(xPos, yPos, 5, 5); } tile(100, 200, color(255,0,0)); //red tile tile(100, 300, color(0,0,255)); //blue tile 6/2/2016© 2010 Larry Snyder, CSE7 Input to the function: x position, y position and color of box

 We’ve seen the function definition … now for the call  The call causes the function to run  Strange Terms:  When they are in the function definition, items in parentheses are called parameters  When they are in the function call, they are called arguments 6/2/2016© 2010 Larry Snyder, CSE8 area = coneArea( 297.5, 100); void tile( int xPos, int yPos){ tile(100, 200);

 The result:  The calls 6/2/2016© 2010 Larry Snyder, CSE9 Just Do It! Better Than Copy/Paste Edit! Better Than Copy/Paste Edit!

 When a function is called it’s like the argument value replaces the corresponding parameter in the code: drawCol(100) 6/2/2016© 2010 Larry Snyder, CSE10 void drawCol( int offset) { // rect(20+100, 50, 60, 20); // Top stone rect(30+100, 70, 40, 10); // Next one ellipse(30+100, 75, 10, 10); // Left curl ellipse(70+100, 75, 10, 10); // Right curl rect(35+100, 80, 30, 60); // Column }

 When a function is called it’s like the parameter is assigned the argument value at the start of the code: drawCol(100) 6/2/2016© 2010 Larry Snyder, CSE11 void drawCol( int offset) { // offset = 100; // rect(20+offset, 50, 60, 20); // Top stone rect(30+offset, 70, 40, 10); // Next one ellipse(30+offset, 75, 10, 10); // Left curl ellipse(70+offset, 75, 10, 10); // Right curl rect(35+offset, 80, 30, 60); // Column }

 By changing arguments we can reuse the function to produce different results … it beats copy/paste/edit! 6/2/2016© 2010 Larry Snyder, CSE12

 Turtles are a concept that deserves a function Parameters: x-position, y-position, goggles 6/2/2016© 2010 Larry Snyder, CSE13 Just Do It!

 So a turtle’s now defined, lets use it! 6/2/2016© 2010 Larry Snyder, CSE14 void raff( float xpos, float ypos) { turtle( xpos, ypos, color(255,0,0)); } void mike( float xpos, float ypos) { turtle( xpos, ypos, color(250,122,0)); } void leo( float xpos, float ypos) { turtle( xpos, ypos, color(0,0,255)); } void don( float xpos, float ypos) { turtle( xpos, ypos, color(128,0,128)); }

 The use of a function to define the four turtles has advantages and disadvantages  Good news  Much shorter specification  The items that change are explicitly noted, params  Ninjas are easy to reposition, and we could give them a standard position, like upper left corner:  Bad news  Can’t drop-and-reassemble – need separate offsets 6/2/2016© 2010 Larry Snyder, CSE15 turtle(-240+xpos,-169+ypos,color(255,0,0));//Raff at (0,0)

 Parameters are automatically declared (and initialized) on a call, and remain in existence as long as the function remains unfinished  When the function ends, the parameters vanish, only to be recreated on the next call  It is wise to choose parameter names, e.g. o-f-f-s-e-t that are meaningful to you  I chose offset as the orientation point of the figure in the x direction  Notice that I used that name a lot, and the meaning to me remained the same 6/2/2016© 2010 Larry Snyder, CSE16