// COMP1405/1005 - Day09 // Functions example // variables float x,y; int count; float myFloat = 0.0; // initialize void setup(){ // the setup() function has no input paramaters and // does not return anything // We use it to initialize variables (and set up the // output window, colors, etc) size(600,400); count = 0; textSize(40); fill(0); frameRate(2); // set frame rate to be 2 frames per second // so that we can see the colour changes easier } // getRandomColor : int -> color // Purpose : function returns a random color // with each R,G,B value bounded above by the // input value // Example : color c = getRandomColor(100); color getRandomColor(int max){ // the function returns a "color" // all functions that have a output/return type must // explicitly "return" a value of the correct type return color( random(max), random(max), random(max)); } // area : float , int -> float // Purpose : computes the area of a rectangle // with length and width defined by // the input values float area(float lengthIn, int widthIn){ float answer; answer = lengthIn * widthIn; return answer ; } // main program void draw(){ if (count < 1){ myFloat = area(0.5, 4); } color theColor = getRandomColor(225); // for fun, let's change the background colour // depending on the RGB values of theColor color bgColor = color( 255-red(theColor), 255-green(theColor), 255-blue(theColor)); background(bgColor); fill( theColor ); text("answer = " + myFloat, 100,100); count += 1; }