Functions Functions Functions are named blocks of code
Functions
Functions • Functions are named blocks of code. • Functions allow complex programs to be broken down into smaller, simpler tasks. • Functions allow commonly used code to be defined once and run again and again.
Think of functions like a black box
Using a Function • A function is called using its name followed by a list of parameters. • We’ve already been using functions that are included in Processing. line(0, 10, 20); Function name Parameter values
Writing a Function • A function is defined by specifying its name, a list of parameters and its return type. • The definition of the line function included in Processing looks something like this: function name return type parameters void line(float x 1, float y 1, float x 2, float y 2) { // line drawing code goes here. . . }
A very simple function Writing a function Calling a function void print. Hello() { println(”Hello”); } void draw() { print. Hello(); } Is draw() a function? ? ? • The function print. Hello() has no parameters and returns no value. • When a function is defined with a return type of void it means that the function doesn’t return anything.
Making a Tree background(0); fill(255); triangle(50, 0, 30, 40, 70, 40); rect(50 -5, 40, 10); • Try this in Processing…
Making a Forest • I want several trees in different locations on the screen. • I could copy and paste the code but I would need to adjust all the numbers for the new locations. • Or… I can just define a function…
Calling draw. Tree() void setup() { draw. Tree(); } Is setup() a function? ? ? void draw. Tree() { fill(255); triangle(50, 0, 30, 40, 70, 40); rect(50 -5, 40, 10) }
This didn’t seem to help much… • Our function still just draws trees at one fixed location. • We need parameters (sometimes called arguments) that allow us to tell the function where we want the tree! • This means we need to figure out the relationship between the position of the tree and the location of its parts.
draw. Tree() With Parameters void draw. Tree(int top. X, int tree. H = 40; int tree. W = 40; triangle(top. X, top. Y, (top. X - tree. W / 2), (top. X + tree. W / 2), rect( (top. X-5), (top. Y } int top. Y) { (top. Y + tree. H), (top. Y + tree. H) ); + tree. H), 10);
Putting It Together void setup() { background(0); fill(255); draw. Tree(50, 0); draw. Tree(75, 50); draw. Tree(20, 40); } void draw. Tree(int top. X, int tree. H = 40; int tree. W = 40; triangle(top. X, top. Y, (top. X - tree. W / 2), (top. X + tree. W / 2), rect( (top. X-5), (top. Y } int top. Y) { (top. Y + tree. H), (top. Y + tree. H) ); + tree. H), 10);
Return Values • Functions such as radians(), sin() and random() return a value. How does that work? • Here is a function max() that returns the max of the two int numbers it receives as parameter values: int max(int number 1, int number 2) { if(number 1 > number 2) { return number 1 } else { return number 2; } }
In-class Exercise • Rewrite the tree() function. Your new tree() function should enable you to specify the width and height of the tree that you create when you call the function. • Write a function to draw a new shape on the canvas multiple times, each at a different position. Be sure to test your function. • Turn your groundhog into a function
- Slides: 14