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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

96<br />

Functions<br />

Figure 3-6. Screen capture of an alternative rendering of<br />

the Many Bouncing Balls program<br />

I mentioned functions briefly in Chapter 2, so let’s look at them again in a little more<br />

detail. Functions add structure <strong>and</strong> flexibility to your sketches, but also a little complexity.<br />

Try not to worry if some of the material seems too abstract—you’ll be using functions<br />

throughout the rest of the book. My main goal here is to give you an overview of basic<br />

programming structures <strong>and</strong> a glimpse into the potential of creative coding.<br />

In the simplest sense, functions just organize code into reusable blocks. They also have the<br />

ability to receive arguments <strong>and</strong> return a value. Here are some examples.<br />

This first sketch draws a rectangle based on the x, y, width, <strong>and</strong> height properties passed to<br />

the drawRectangle() function:<br />

void setup(){<br />

size(400, 400);<br />

background(255);<br />

drawRectangle(150, 150, 100, 100);<br />

}<br />

void drawRectangle(float x, float y, float w, float h){<br />

rect(x, y, w, h);<br />

}

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

Saved successfully!

Ooh no, something went wrong!