/* 6.914, Example 4.2 Methods are ways of executing certain statement and (optionally) returning information. Unlike the setup(), draw() and mouse...() methods, they do not happen by default. You must call them. The simplest type of method is a void method, which does not return any type of information. You can create a void method by naming the method (use lowerCamelCase) in this way: void methodName(){ // some code goes here // this code runs when you call "methodName();" in draw(), setup(), // or anywhere else in your code. } You can send parameters to your method, which will allow it to process certain types of information. You can specify what types of information it will take. Just like the way that background() takes three colors, for instance, you could have a method that took two ints and printed out the result of multiplying them: void methodName(int myFirstInt, int mySecondInt){ println("The result is "+(myFirstInt*mySecondInt)); } You can pass any type of object to a method, so the method can interact with it: void methodName(int myInt, boolean myBool, String myString){ if (myBool){ println("The string is: "+myString); } else { println("The int is: "+myInt); } } You can also have a method return a certain type of information. This means that the method will execute some code and produce a result. You can use this result by setting some variable equal to the method. For instance, the method dist() returns a float. To make a method with a certain return type, use that return type (one of the variable types we've talked about: int, boolean, String, etc) instead of 'void' when creating your method. You will also need a "return x;" statement somewhere in your code, where 'x' is an object of the return type you specified. So, if you wanted a method that returned a random int, you could use this: int methodName(){ return int(random(0, 10)); // returns a random int between 0 and 9 } Non-void methods can take parameters too. This method adds two ints together: int methodName(int firstInt, int secondInt){ return (firstInt + secondInt); } You must have a return statement in any case. Return will cause the method to stop running, so if you have a return statement inside an if statement that won't automatically run, you must also have one outside the if statement in case the first one isn't executed. int methodName(boolean doAdding, int firstInt, int secondInt){ if (doAdding){ return (firstInt + secondInt); } return secondInt; // make sure some return statement will be hit } You can return all of our object types: boolean methodName(int firstInt, int secondInt){ if (firstInt + secondInt < 5){ return false; } else { return true; // works because either 'if' or 'else' will always be reached } } You can run many different types of commands inside your methods. */ void setup(){ size(200, 200); noLoop(); smooth(); } void draw(){ background(255); drawDiagonal(5); drawDiagonal(15); drawEllipse(); // drawing diamond using centerX, centerY, horizAxis and vertAxis fill(0, 0, 255); drawDiamond(50, 50, 20, 30); // showing where center of diamond is strokeWeight(3); stroke(0); point(50, 50); // try passing a different boolean parameter to getStatement String statementToPrint = getStatement(true); } void drawDiagonal(float startingX){ stroke(random(255), 0, 0); line(startingX, 0, startingX + 15, 20); } void drawEllipse(){ noStroke(); fill(155); ellipse(random(100, 140), 100, 20, 20); } String getStatement(boolean myBool){ if (myBool){ return "This is the truth."; } else { return "This is not the truth."; } } void drawDiamond(float centerX, float centerY, float horizAxis, float vertAxis){ quad(centerX, centerY - vertAxis/2, centerX + horizAxis/2, centerY, centerX, centerY + vertAxis/2, centerX - horizAxis/2, centerY); }