Chapter 9: Functions

  1. Function Basics
    1. Example 9-1: Roll The Dice
    2. Example 9-2: Another Way to Roll
  2. Make a Function
    1. Example 9-3: Draw the Owl
    2. Example 9-4: Two's Company
    3. Example 9-5: An Owl Function
    4. Example 9-6: Increasing the Surplus Population
    5. Example 9-7: Owls of Different Sizes
  3. Return Values
    1. Example 9-8: Return a Value
  4. Robot 7: Functions

Functions

Function are huge! They are cool! They are powerful! They are the software equivolent of a robot! Build them, and thy keep working! They can do 'things' automatically, forever. Build it once, the you can use it to build other things (like robots). They take the following general form (in psuedo code):

  dataTypeReturned functionName() {
    // Things to do
  }

A working example:

PROCESSING

Example

void setup() {
  println(addMe(10,20));
}

int addMe(int val1, int val2) {
  return(val1 + val2);
}

Function Basics

Functions take the same form/syntax as the setup() and draw() functions built into Processing.

PROCESSING

Example 9-1: Roll the Dice

void setup() {
  println("Ready to roll!");
  rollDice(20);
  rollDice(20);
  rollDice(6);
  println("Finished.");
}

void rollDice(int numSides) {
  int diceVal = 1 + int(random(numSides));
  println("Rolling... " + diceVal);
}

Result

PROCESSING

Example 9-2: Another Way To Roll

void setup() {
  println("Ready to roll!");
  int diceVal1 = 1 + int(random(20));
  println("Rolling... " + diceVal1);
  int diceVal2 = 1 + int(random(20));
  println("Rolling... " + diceVal2);
  int diceVal3 = 1 + int(random(6));
  println("Rolling... " + diceVal3);
  println("Finished.");
}

void rollDice(int numSides) {
  int diceVal = 1 + int(random(numSides));
  println("Rolling... " + diceVal);
}

Result

Make A Function

Let's draw an Owl in a function using the above components:

PROCESSING

Example 9-3: Draw The Owl

void setup() {
  size(480, 120);
}

void draw() {
  background(176, 204, 226);
  translate(110, 110);
  stroke(138, 138, 125);
  strokeWeight(70);
  line(0, -35, 0, -65); // Body
  noStroke();
  fill(255);
  ellipse(-17.5, -65, 35, 35);  // Left eye dome
  ellipse(17.5, -65, 35, 35);   // Right eye dome
  arc(0, -65, 70, 70, 0, PI);   // Chin
  fill(51, 51, 30);
  ellipse(-14, -65, 8, 8);  // Left eye
  ellipse(14, -65, 8, 8);   // Right eye
  quad(0, -58, 4, -51, 0, -44, -4, -51); // Beak
}

Result

 

PROCESSING

Example 9-4: Two's Company

void setup() {
  size(480, 120);
}

void draw() {
  background(176, 204, 226);
  
  // Left Owl
  translate(110, 110);
  stroke(138, 138, 125);
  strokeWeight(70);
  line(0, -35, 0, -65); // Body
  noStroke();
  fill(255);
  ellipse(-17.5, -65, 35, 35);  // Left eye dome
  ellipse(17.5, -65, 35, 35);   // Right eye dome
  arc(0, -65, 70, 70, 0, PI);   // Chin
  fill(51, 51, 30);
  ellipse(-14, -65, 8, 8);  // Left eye
  ellipse(14, -65, 8, 8);   // Right eye
  quad(0, -58, 4, -51, 0, -44, -4, -51); // Beak
  
  // Right Owl
  translate(70, 0);
  stroke(138, 138, 125);
  strokeWeight(70);
  line(0, -35, 0, -65); // Body
  noStroke();
  fill(255);
  ellipse(-17.5, -65, 35, 35);  // Left eye dome
  ellipse(17.5, -65, 35, 35);   // Right eye dome
  arc(0, -65, 70, 70, 0, PI);   // Chin
  fill(51, 51, 30);
  ellipse(-14, -65, 8, 8);  // Left eye
  ellipse(14, -65, 8, 8);   // Right eye
  quad(0, -58, 4, -51, 0, -44, -4, -51); // Beak
}

Result

 

PROCESSING

Example 9-5: An Owl Function

void setup() {
  size(480, 120);
}

void draw() {
  background(176, 204, 226);
  drawOwl(110, 110);
  drawOwl(180, 110);
}

void drawOwl(int xPosition, int yPosition) {
  pushMatrix();
    translate(xPosition, yPosition);
    stroke(138, 138, 125);
    strokeWeight(70);
    line(0, -35, 0, -65); // Body
    noStroke();
    fill(255);
    ellipse(-17.5, -65, 35, 35);  // Left eye dome
    ellipse(17.5, -65, 35, 35);   // Right eye dome
    arc(0, -65, 70, 70, 0, PI);   // Chin
    fill(51, 51, 30);
    ellipse(-14, -65, 8, 8);  // Left eye
    ellipse(14, -65, 8, 8);   // Right eye
    quad(0, -58, 4, -51, 0, -44, -4, -51); // Beak
  popMatrix();
}

Result

 

PROCESSING

Example 9-6: Increasing the Surplus Population

void setup() {
  size(480, 120);
}

void draw() {
  background(176, 204, 226);
  for(int xVal = 35; xVal < width + 70; xVal += 70) {
    drawOwl(xVal, 110); 
  }
}

void drawOwl(int xPosition, int yPosition) {
  pushMatrix();
    translate(xPosition, yPosition);
    stroke(138, 138, 125);
    strokeWeight(70);
    line(0, -35, 0, -65); // Body
    noStroke();
    fill(255);
    ellipse(-17.5, -65, 35, 35);  // Left eye dome
    ellipse(17.5, -65, 35, 35);   // Right eye dome
    arc(0, -65, 70, 70, 0, PI);   // Chin
    fill(51, 51, 30);
    ellipse(-14, -65, 8, 8);  // Left eye
    ellipse(14, -65, 8, 8);   // Right eye
    quad(0, -58, 4, -51, 0, -44, -4, -51); // Beak
  popMatrix();
}

Result

PROCESSING

Example 9-7: Owls of Different Sizes

void setup() {
  size(480, 120);
}

void draw() {
  background(176, 204, 226);
  randomSeed(0);
  for(int i = 35; i < width + 40; i += 40) {
    int randomGray = int(random(0, 102));
    float randomScale = random(0.25, 1.0);
    drawOwl(i, 110, randomGray, randomScale);
  }
}

void drawOwl(int xPosition, int yPosition, int grayVal, float scaleVal) {
  pushMatrix();
    translate(xPosition, yPosition);
    scale(scaleVal); // Set the size
    stroke(138-grayVal, 138-grayVal, 125-grayVal); // Set the color value
    strokeWeight(70);
    line(0, -35, 0, -65); // Body
    noStroke();
    fill(255);
    ellipse(-17.5, -65, 35, 35);  // Left eye dome
    ellipse(17.5, -65, 35, 35);   // Right eye dome
    arc(0, -65, 70, 70, 0, PI);   // Chin
    fill(51, 51, 30);
    ellipse(-14, -65, 8, 8);  // Left eye
    ellipse(14, -65, 8, 8);   // Right eye
    quad(0, -58, 4, -51, 0, -44, -4, -51); // Beak
  popMatrix();
}

Result

Return Values

Article content

PROCESSING

Example 9-8: Return A Value

void setup() {
  float yourWeight = 132;
  float marsWeight = calculateMars(yourWeight);
  println(marsWeight);
}

float calculateMars(float weightVal) {
  float newWeight = weightVal * 0.38;
  return newWeight;
}

Result

Robot 7

Let's put our robot into a robot drawing function...

PROCESSING

Robot 7: Functions

void setup() {
  size(720, 480);
  strokeWeight(2);
  ellipseMode(RADIUS);
}

void draw() {
  background(0, 153, 204);
  drawRobot(120, 420, 110, 140);
  drawRobot(270, 460, 260, 95);
  drawRobot(420, 310, 80, 10);
  drawRobot(570, 390, 180, 40);
}

void drawRobot(int xVal, int yVal, int bodyHeight, int neckHeight) {
  int radius = 45;
  int neckYVal = yVal - bodyHeight - neckHeight - radius;
 
  // Neck
  stroke(255);
  line(xVal+2, yVal-bodyHeight, xVal+2, neckYVal);
  line(xVal+12, yVal-bodyHeight, xVal+12, neckYVal);
  line(xVal+22, yVal-bodyHeight, xVal+22, neckYVal);
 
  // Antennae
  line(xVal+12, neckYVal, xVal-18, neckYVal-43);
  line(xVal+12, neckYVal, xVal+42, neckYVal-99);
  line(xVal+12, neckYVal, xVal+78, neckYVal+15);
  
  // Body
  noStroke();
  fill(255, 204, 0);
  ellipse(xVal, yVal-33, 33, 33);
  fill(0);
  rect(xVal-45, yVal-bodyHeight, 90, bodyHeight-33);
  fill(255, 204, 0);
  rect(xVal-45, yVal-bodyHeight+17, 90, 6);
  
  // Head
  fill(0);
  ellipse(xVal+12, neckYVal, radius, radius);
  fill(255);
  ellipse(xVal+24, neckYVal-6, 14, 14);
  fill(0);
  ellipse(xVal+24, neckYVal-6, 3, 3);
  fill(153, 204, 255);
  ellipse(xVal, neckYVal-8, 5, 5);
  ellipse(xVal+30, neckYVal-26, 4, 4);
  ellipse(xVal+41, neckYVal+6, 3, 3);
}
Resulting Display Window

Previous Chapter
Next Chapter