Presentation is loading. Please wait.

Presentation is loading. Please wait.

Coding – Week 2 Functions, Arrays, and Objects. Functions  Functions are not a new concept – you’ve been using them already.  void setup() {} and void.

Similar presentations


Presentation on theme: "Coding – Week 2 Functions, Arrays, and Objects. Functions  Functions are not a new concept – you’ve been using them already.  void setup() {} and void."— Presentation transcript:

1 Coding – Week 2 Functions, Arrays, and Objects

2 Functions  Functions are not a new concept – you’ve been using them already.  void setup() {} and void draw() {} are both functions, just Processing calls them automatically (at the beginning, and on every frame, of the program, respectively).  You can define your own functions, almost the same way, but they won’t be used automatically. You call them by writing the name of the function, followed by parentheses. void myFunction() { // Defines a function. // Set of instructions here (‘Do stuff’) } […] void draw() { // Defines your main loop, where all your code is run. myFunction(); // Runs the set of instructions (‘Does the stuff.’) }

3 Arguments  Remember: Everything is important.  Why the parentheses for functions? Because you can use arguments, which change the functions before you call them. void myFunction(string t) {// Defines the function text(50,50,t); } […] myFunction(“Draw this text”); // Calls the function  This does nothing but draw the text within the parentheses at. The set of instructions within the function uses the argument as a variable. You are setting that argument/variable when you call the function.

4 Functions that ‘Return’ Variables  The ‘void’ in a function definition just means this function won’t return any variables.  If a function does return a variable, it can be used in place of a variable of the class the function returns (integer, float, string, boolean, etc.)  If a function returns a variable, you write the class in place of ‘void’, and that function must return a variable of that class. int ReturnFive() { return 5; }  If used in place of a variable, whatever the function ‘returns’ will be used as the variable (i.e.: variable = ReturnFive() is identical to saying variable = 5)  You can do more complex operations to return more meaningful numbers, like mathematical functions that return the square root of something, etc.  This can all be combined. For example, you can use a return function as the argument of a function call ( example: GetSquareRoot( ReturnFive() ); )

5 Arrays  Remember what a variable is – an object that stores a single bit of information, like a number, a string of letters, a boolean value (true/false), etc. Think of it like a box.  An array is like a row of boxes, each holding a bit of information, exactly like a variable would. (Like this.)

6 More Arrays  The information arrays can store is just like those of variables. int[] numbers = new int[7]  Here we have an array with 7 indexes, which are all going to hold integers. This is almost identical to int number = 5. A variable is just a single ‘box’, holding one integer (in that case, 5).  We can set each ‘box’/index of the array, just like variables, like so: numbers[0] = 6;  This sets the 0 th space in the array.  Arrays start at 0, so one with 7 indexes/boxes will go from 0 – 6. Emphasis.  If you try to set a space that’s not in the array (i.e. not 0 – 6 ), you’ll get an error (so you can’t set numbers[7] in this example).

7 Arrays, Arrays, Arrays  For deciding what pill to take for the day: TakePill ( pills[dayofweek] ); TakePill() A function for taking a pill. pills[] the box of pills (an array with 7 indexes/boxes). dayofweek the day of the week, where 0 = Sunday, 1 = Monday, etc.  If ‘dayofweek’ didn’t start at 0, you would try to access pills[7] on Saturday, which gives you an error because pills[] only goes from 0-6. Then you’re off your meds, and before you know it you’re giving lectures about abstract concepts that don’t mean anything and nobody understands a word you’re saying, but they’re too shy to correct you.  This is what I wanted to show in the lesson on loops, but couldn’t: for (int i = 0; i < numbers.length; i++) { numbers[i] = 1; }  This will set every space in the array to 1.

8 I’m Gonna Talk About Arrays Until You Like It  This is useful for affecting multiple variables at once. For example, instead of: number1 = 1; number2 = 2; number3 = 3;  You can write: numbers[0] = 0; numbers[1] = 1; for (int i = 2; i < numbers.length; i++) { numbers[i] = numbers[i-1]+numbers[i-2]; // Assuming ‘numbers’ is an array of integers. }  This does the exact same thing, but you can make the array bigger or smaller, and it doesn’t matter if there are 3 variables, or 300. numbers.length is a variable that returns the size of the ‘numbers’ array automatically, so it will loop through all indexes/boxes in the array.  Yes, there totally are places where you would use this. Writing a function to affect every car on the streets in GTAV, for example. Games typically save all objects/instances of a class to arrays.

9 Object Classes  Wonder why I don’t give you an authoritative list of variable types (or classes)? Like integers, floats, strings, booleans, etc...  Because you can create your own.  These can be… well… anything. You could create a class for cars, NPCs, clouds of smoke, or fireballs… whatever could be described using discrete entities/instances.  Variables can hold instances of the objects you define. So you can define NPCs, and keep a list of NPC instances, and do things that affect every instance (like AI functions), etc.

10 Instances of Object Classes  An instance is just ‘an example or single occurrence of something’. You (as in you, person reading these words) are an instance of a human being. This is an instance of a lecture.  The object class is basically the abstract form these instances are derived from – the concept and definition of a ‘human being’ is different from the actual instances of human beings.  You (yes, you) do not define a human being, and what you do as a human is completely independent of other humans. To refer to you as ‘human’ just means you share in the class of human beings.  An instance is what a variable can contain. It won’t contain the entire object class.

11 Defining Object Classes  You define an object/class much like defining a function. class myObject {// Defining the class (note: class instead of void, // and no parentheses, because no arguments) int x, y; // Variables inside of your object. myObject(int a, int b) { // Instance constructor (a function; does take arguments) // (This defines how new instances are created! Think of it as // a function returning an instance) x = a; y = b;// Sets the x and y coordinates for the newly constructed // instance, using the arguments of the function. } void update() {// A function inside the object/class x = mouseX; y = mouseY; // Also sets the x and y coordinates }

12 Using Objects/Classes  myObject variable = new myObject(5,5);  This is going to store a new instance of the object in the variable.  The constructor is what is being used here. This is a function.  The arguments set the x and y of the new instance we’re creating, using the arguments.  You could then use variable.update(); to run the update() function within the object instance (indicated within the variable), which sets its x and y to the mouse coordinates. You can do this in the draw loop to have the instance update every frame, like everything else, or call the function at specific times (for example, if the object is an NPC/game character and its health reaches zero, you can call a function within it to destroy it).

13 Objects and Arrays  You can of course put objects into arrays, thus having an array of objects. myObject[] objects = new myObject[5]; // No instances are created yet.  This defines an array of the object class, which you can set the same as any array. objects[3] = new myObject(10,10); // Instance stored in array.  This sets the fourth space of the objects array to be a new instance of the object class ‘myObject’, with the arguments of its instance constructor being 10 and 10, to set its enclosed x and y coordinates to.  Read that carefully and stop me if makes no goddamn sense.  Seriously. Do it now.

14 That’s It.  That is all of the most basic fundamentals of code. All of them. Every one.  This was extremely quick, as you may have noticed. I wanted to show you how much you’ll have to deal with, and the full extent of what you’re expected to learn.  You can consider this ‘basics of programming’ – everything you need to get started. The rest is optional, but I would be tremendously happy to teach it.  We will continue to go through the Shiffman text, but it all hinges on this, which you can henceforth refer to as the Super Important Stuff.  Ask me about anything you have trouble with. You will not look stupid. Most classes take weeks to get through just one of these concepts.  That said, go to the lab and try this out. I’ll post these notes right away.


Download ppt "Coding – Week 2 Functions, Arrays, and Objects. Functions  Functions are not a new concept – you’ve been using them already.  void setup() {} and void."

Similar presentations


Ads by Google