09.04.2016 Views

www.ebook777.com

Make_Getting_Started_with_Processing_Second_Edition

Make_Getting_Started_with_Processing_Second_Edition

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.

called calculateMars() that calculates the weight of a person or<br />

object on our neighboring planet:<br />

void setup() {<br />

float yourWeight = 132;<br />

float marsWeight = calculateMars(yourWeight);<br />

println(marsWeight);<br />

}<br />

float calculateMars(float w) {<br />

float newWeight = w * 0.38;<br />

return newWeight;<br />

}<br />

Notice the data type float before the function name to show<br />

that it returns a floating-point value, and the last line of the<br />

block, which returns the variable newWeight. In the second line of<br />

setup(), that value is assigned to the variable marsWeight. (To<br />

see your own weight on Mars, change the value of the your<br />

Weight variable to your weight.)<br />

Robot 7: Functions<br />

In contrast to Robot 2 (see “Robot 2: Variables” on page 47),<br />

this example uses a function to draw four robot variations within<br />

the same program. Because the drawRobot() function appears<br />

Functions 131

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

Saved successfully!

Ooh no, something went wrong!