Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions. Functions are named blocks of code. Functions allow complex programs to be broken down into smaller, simpler tasks. Functions allow commonly.

Similar presentations


Presentation on theme: "Functions. Functions are named blocks of code. Functions allow complex programs to be broken down into smaller, simpler tasks. Functions allow commonly."— Presentation transcript:

1 Functions

2 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.

3 Think of functions like a black box

4 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, 10, 20); Function name Parameter values

5 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: void line(float x1, float y1, float x2, float y2) { // line drawing code goes here... } return type function name parameters

6 A very simple function The function printHello() 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. void draw() { printHello(); } void printHello() { println(”Hello”); } Writing a functionCalling a function Is draw() a function???

7 Making a Tree background(0); fill(255); triangle(50,0,30,40,70,40); rect(50-5,40,10,10); Try this in Processing…

8 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…

9 Calling drawTree() void setup() { drawTree(); } void drawTree() { fill(255); triangle(50,0,30,40,70,40); rect(50-5,40,10,10) } Is setup() a function???

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.

11 drawTree() With Parameters void drawTree(int topX, int topY) { int treeH = 40; int treeW = 40; triangle(topX, topY, (topX - treeW / 2), (topY + treeH), (topX + treeW / 2), (topY + treeH) ); rect( (topX-5), (topY + treeH),10,10); }

12 Putting It Together void setup() { background(0); fill(255); drawTree(50,0); drawTree(75,50); drawTree(20,40); } void drawTree(int topX, int topY) { int treeH = 40; int treeW = 40; triangle(topX, topY, (topX - treeW / 2), (topY + treeH), (topX + treeW / 2), (topY + treeH) ); rect( (topX-5), (topY + treeH),10,10); }

13 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 number1, int number2) { if(number1 > number2) { return number1 } else { return number2; }

14 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


Download ppt "Functions. Functions are named blocks of code. Functions allow complex programs to be broken down into smaller, simpler tasks. Functions allow commonly."

Similar presentations


Ads by Google