04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

If you run this, you should see a 100-by-100-pixel square in the middle of the display<br />

window.<br />

When you write your own function, you need to include <strong>Processing</strong>’s setup() function, or<br />

you’ll get a compile error when you try to run the sketch. A function begins with a return<br />

type (either void or a legal data type); an identifier (the name of the function); open <strong>and</strong><br />

closed parentheses, with any optional declared parameters; <strong>and</strong> open <strong>and</strong> closed curly<br />

braces.<br />

Here’s the structure of a function:<br />

return type (void or legal datatype) function name(optional ➥<br />

parameters){<br />

code to execute when function is called;<br />

}<br />

The drawRectangle(float x, float y, float w, float h) function in the preceding<br />

sketch expects four arguments to be passed to it when it’s called, as I specified four<br />

parameters between its parentheses. You’ll notice that I use both the terms parameter <strong>and</strong><br />

argument. This can be confusing to new coders (<strong>and</strong> even some experienced coders); I<br />

prefer to call the values between the parentheses in the function definition parameters,<br />

<strong>and</strong> the values passed to the function arguments. I know this may sound like an issue of<br />

semantics (or worse), but I find it easier to discuss <strong>and</strong> teach this stuff by using the two different<br />

terms.<br />

This last example didn’t do much more than <strong>Processing</strong>’s plain old rect() call. The next<br />

example (see Figure 3-7) makes better use of the drawRectangle() function <strong>and</strong> also<br />

revisits for loops—yippee!<br />

void setup(){<br />

size(400, 400);<br />

background(255);<br />

for (int i=0; i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!