Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions. 2 Modularity What is a function? A named block of code Sometimes called a ‘module’, ‘method’ or a ‘procedure’ Some examples that you know are:

Similar presentations


Presentation on theme: "Functions. 2 Modularity What is a function? A named block of code Sometimes called a ‘module’, ‘method’ or a ‘procedure’ Some examples that you know are:"— Presentation transcript:

1 Functions

2 2 Modularity What is a function? A named block of code Sometimes called a ‘module’, ‘method’ or a ‘procedure’ Some examples that you know are: setup()background() draw()ellipse() mousePressed()rect() Some functions are ‘built-in’ You can write your own functions (user-defined functions).

3 3 Why use functions? Is your draw() method getting a bit long? Two key principles of good programming: 1) Modularity Break down code into smaller parts More manageable and readable Reduce the number of local variables inside a module 2) Reusability Duplicated code (copy/paste?) is not good Must maintain in multiple places Better to put duplicate code in a new function and ‘call’ it from multiple places

4 The Function ‘Definition’ Make a named ‘block’ with the following parts: Return type Function name Parameters area start block void drawCircle ( ) { // Variables and code go here } // end drawCircle This is also called ‘declaring’ the function. Other Rules: Define function outside all other functions. Name with the same rules as variables (letters, digits, _) Do not use ‘reserved’ words or already used names 4

5 Example void drawCircle() { fill(0); ellipse(mouseX, mouseY, 50, 50); } 5

6 Calling a Function Now that we have a function, let’s use it: void draw() { background(255); drawCircle(); } You ‘call’ functions from ‘inside’ other functions In this case, inside draw() 6

7 Bouncing Ball without Functions Group code into related ‘chunks’ 7

8 Bouncing Ball with Functions Name the ‘chunks’, declare functions, call them 8

9 Code is well-organized and readable. Greater ease in debugging. void draw() { background(255); // move(); // bounce(); display(); } 9

10 Functions with Arguments and Parameters What if you wanted to use a function to do slightly different things? Some examples you have seen that pass arguments to functions include: size(200,200); color(100,150,0); ellipse(x, y, width, height); What if you wanted to write your own function that receives parameters? 10

11 Arguments (Local Variables) 11 // define the function void drawCircle(int diameter) { fill(0); ellipse(mouseX, mouseY, diameter, diameter); } // call the function drawCircle (50); drawCircle (25); Question: How can you draw the circle with random size?

12 Multiple Arguments 12 void setup() { size (200, 200); } void draw() { background(255); // Pass drawCircle four arguments drawCircle ( 100,100,64, color(200,200,0) ); drawCircle ( 50,75,32, color(0,200,100) ); drawCircle ( 80,175,40, color(200,0,0) ); } // drawCircle receives four parameters void drawCircle(int x, int y, int diameter, color c) {... }

13 Keys to Passing Arguments You must pass the same number of arguments as defined in the function parameter list. When an argument is passed, it must be of the same type as declared within the parameters in the function definition. An integer must be passed into an integer a floating point into a floating point.. The value you pass as an argument to a function can be a: Literal value (20, 5, 4.3, etc.), Variable (x, y, size, etc.) The result of an expression (8 + 3, 4 * x/2, random(0,10), etc.) Parameters act as local variables inside the receiving function They are only accessible within that function 13

14 Return types void setup() { void draw() { void drawCircle(int x, int y, int diameter, color c) { Here is an example that ‘return’ an int: int sum(int a, int b, int c) { int total = a + b + c; return total; } As soon as the return statement is executed, the program exits the function and sends the returned value. 14

15 A useful example: Distance between 2 pts 15 float distance(float x1, float y1, float x2, float y2) { float dx = x1 – x2; // one side of the right triangle float dy = y1 – y2; // other side of the right triangle float d = sqrt(dx*dx + dy*dy); // hypotenuse length return d; } Processing has a dist() function that does the same thing.

16 Exercise Use the distance function to calculate a brightness value for each quadrant. 16

17 Exercise: Bouncing Circle 17 int circleX = 25; int circleY = 25; int speedX = 1; int speedY = 1; void setup() { size(600,200); } void draw() { background(255);... } void moveCircle(){... } void drawCircle(int x, int y, int diameter) {... }


Download ppt "Functions. 2 Modularity What is a function? A named block of code Sometimes called a ‘module’, ‘method’ or a ‘procedure’ Some examples that you know are:"

Similar presentations


Ads by Google