Course Home | Documentation | Lab Hours/Tutoring | Projects | Quizzes | Schedule | Submit

# Module 09 : User-defined Functions

## Topics

• User-defined functions

#### New processing syntax:

• Basics of a function signature:
```    returnType functionName(parameters) {
functionBody
}
```

## Resources

• Watch Daniel Shiffman Video 7.1, Function Basics (9 minutes)

• Watch Daniel Shiffman Video 7.2, Modularity with Functions (9 minutes)

• Watch Daniel Shiffman Video 7.3, Reusability with Functions (7 minutes)

• Watch Daniel Shiffman Video 6.3, Variable Scope (9 minutes)

• Ch. 9 of the Reas/Fry book (pp. 121-133) is good for motivation (but they are using some techniques that we haven't yet seen from Chapter 6 of that book.

## In-Class Activities

Try to recreate any of the following interactive sketches on your own.

Flags
Implement a function that has signature as follows.

```void flag(int x, int y, int w, int h, color a, color b, color c)
```
which is responsible for rendering the image of a flag with top-left corner at (x,y), given width and height, and three evenly sized horizontal stripes with colors a, b, and c, respectively, from top to bottom. With a correct implementation, the sketch
```void setup() {
size(450, 100);
flag(25,25,80,60,color(0), color(227,0,19), color(255,205,70));     // Germany
flag(125,25,80,60,color(255), color(7,32,245), color(255,0,23));    // Russia
flag(225,25,90,60,color(178,25,43), color(255), color(31,72,135));  // Netherlands
flag(325,25,90,60,color(243,36,61), color(255), color(243,36,61));  // Austria
}
```
will produce the following image.

Implementation: spoiler

Implement a function that has signature as follows.

```void ladder(int x, int y, int w, int h, int n)
```
which is responsible for rendering the image of a ladder having top-left corner at (x,y), total width w and height h, and then n evenly spaced rungs between the extremes. With a correct implementation, the sketch
```void setup() {
size(300, 400);
}
```
will produce the following image.

Implementation: spoiler

Caterpillar
Implement a function that has signature as follows.

```void caterpillar(int x, int y, int r, int n)
```
which is responsible for rendering the image of a caterpillar having n overlapping body segments, each with radius r, and with the head at the left end, centered at (x,y). The body segment centers should also be r pixels apart. We allow the fill color to be set outside the function, but care will be needed to get eyes filled with black while not having a lasting effect on the fill color or stroke weight. For example, note that in the following sketch
```void setup() {
size(500, 200);
fill(0, 200, 0);
caterpillar(50, 50, 20, 10);
caterpillar(50, 100, 10, 5);
fill(255, 255, 0);
caterpillar(100, 150, 10, 10);
}
```
both the first and second caterpillars are green and the third is yellow, producing the following image.

Implementation: spoiler

Random Colors
We have seen several examples where we wanted to use a randomly chosen color for some purpose. The syntax for doing so is a bit bulky, and if you wanted to use random colors in several different places within your code, it might be worthwhile to implement a function that could be called with syntax randomColor() and which would return the random color. Implement such a function and then demonstrate its use to complete the following sketch:

```void setup() {
size(500,400);
strokeWeight(5);
}

void draw() {
fill(randomColor());
stroke(randomColor());
ellipse(random(width), random(height), 30, 30);
}

```

Implementation: spoiler

Growing Smiley
A varient of last module's growing circle. This time we use a funciton that draws a smiley face with a given center and scale. To get you going, we provide our code that implements a drawFace function and a drawFaceMessage function. See if you can use those to achieve the growing effect.

Implementation: spoiler

Sunrise, Sunset
For our next example, we begin by providing a static sketch of a scene with a house (house.pde). Your goal is to redesign the code so that it toggles between daytime and nighttime with each mouse click. We would like you to use separate functions for the two versions, but ideally to have yet another function that encapsulates all of the commands that are common to both versions.

Implementation: spoiler

## Sample Quiz Question

Michael Goldwasser
CSCI 1050, Spring 2016