Fun with Functions

Fun with Functions
Functions

Here is the form of a function definition
<return type> <name> ( <param list> ) {
<body>
}

Example:
void draw_box(int x_pos, int y_pos) {
rect(x_pos, y_pos, 20, 20);
}
color pinkish() {
return color(255, 200, 180);
}
Writing Functions
●
Processing function definitions
typically go after standard blocks
like setup(), draw(),
mousePressed(), etc.
Function call
Function definition
Using Functions
●
Once defined, you can call
functions repeatedly...
that's the point of writing
them!
Function calls
Function definition
Arguments Become Parameters
●
●
Notice that:
–
if the definition has n parameters
–
the call needs n arguments
Parameters and arguments
correspond
Inside the function, the
parameter (e.g. xbase) is
declared and initialized to
the corresponding argument
(e.g. 80 for the last call).
Then the function can use it:
rect(80, 20+ 10, 20, 40)
Example: Clock Timer
Define hexa() and rexa()
●
Pattern: parameterize
functions by a consistent
position - upper left
corner is often convenient
Where is (xbase, ybase)?
void hexa(float xbase, float ybase) {
rect(xbase, ybase - 40, 20, 40);
triangle(xbase, ybase,
xbase + 20, ybase,
xbase + 10, ybase + 10);
triangle(xbase, ybase - 40,
xbase + 20, ybase - 40,
xbase + 10, ybase - 50);
}
A
void setup() {
...
hexa(x, y);
fill(255);
ellipse(x, y, 5, 5);
}
B
C
D
Where is (xbase, ybase)?
void rexa(float xbase, float ybase) {
triangle(xbase, ybase,
xbase + 10, ybase - 10,
xbase + 10, ybase + 10);
rect(xbase + 10, ybase - 10, 40, 20);
triangle(xbase + 50, ybase - 10,
xbase + 50, ybase + 10,
xbase + 60, ybase);
}
A
void setup() {
...
rexa(x, y);
fill(255);
ellipse(x, y, 5, 5);
}
B
C
D
Forming a Digit (reuse)
Let there be light (and dark)
●
Define the illumination of the
digit
–
Declare two color variables,
initialize them to proper colors,
use them as fill colors
Counting with Lights:
A function for each number
●
A function for each numeral, lights up the
proper parts of the digit
Select a number
●
Given an integer, light it up
3-digit display
What is 328 % 10 ?
A. 3
B. 2
C. 8
D. 32
E. 33
What is (328 / 10) % 10 ?
A. 3
B. 2
C. 8
D. 32
E. 33
Count up at the frame rate
Functional Abstraction
●
Review of what we did:
The program is ONLY
drawing triangles and
rectangles, but we
don't think of it that
way... to us it is a timer
three_digit
sel
digit
one
two
three
four
hexa
five
six
rexa
rect
triangle
Our tower of functions!
seven eight
nine
zero
Key Points
●
Functional abstraction reduces complexity
●
Functions can be built up out of functions
–
●
Tower of definitions
With functions you can think about your
program at a higher level
Invaders!
●
Jared Tarbell (1973-)
complexification.net
Grid of pixels
What does the code do?
A. A 5x5 grid of solid pixels
B. A 5x5 grid of pixels with
every second pixel not
drawn
C. A 5x5 grid of pixels with
each pixel randomly drawn
or not drawn
D. A 5x5 grid of pixels that
flicker as they are randomly
drawn and redrawn quickly
What does the code do?
A. A 3x5 grid of pixels
randomly drawn or not drawn
B. A 5x5 grid of pixels with
each pixel randomly drawn or
not drawn
C. A 3x5 grid of pixels that are
twice as wide as they are high
D. A 3x5 grid of pixels that are
randomly drawn or not drawn,
mirrored horizontally
Functional Invaders
Lots'o invaders
Recursive Shield
End