Download presentation
Presentation is loading. Please wait.
Published byGertrude Newton Modified over 8 years ago
1
Lawrence Snyder University of Washington, Seattle © Lawrence Snyder 2004 Assignment 6
2
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 …
3
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© 2010-2013 Larry Snyder3
4
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© 2010-2013 Larry Snyder4
5
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
6
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); }
7
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
8
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);
9
The result: The calls 6/2/2016© 2010 Larry Snyder, CSE9 Just Do It! Better Than Copy/Paste Edit! Better Than Copy/Paste Edit!
10
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 }
11
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 }
12
By changing arguments we can reuse the function to produce different results … it beats copy/paste/edit! 6/2/2016© 2010 Larry Snyder, CSE12
13
Turtles are a concept that deserves a function Parameters: x-position, y-position, goggles 6/2/2016© 2010 Larry Snyder, CSE13 Just Do It!
14
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)); }
15
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)
16
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.