Sunday, September 25, 2011

Class 4: Arrays, Functions and using Images

Today's class will cover two very important concepts: Arrays and Functions. Afterwards, I will show you how use images in your sketches.

An array is a list or group of data. You can have an array of any type of data. For example, you want to move 10 rectangles around the screen using random incremental movements between -1 and 1(brownian motion). Instead of creating 10 variables for the x location and 10 for the y location, you can use two arrays, one for the x locations and 1 for the y locations. All of the variables are the same type and their purpose is the same.

Each variable(variables in an array are referred to as elements) in an array is associated with an index number that describes in position in the array. The first element in an array is [0] and the second is [1] and so on. When you create an array you must identify the length or number of elements in the array. You cannot access a value higher than the length of an array. If you try you will receive the Null Pointer error.

Arrays are a little different to create than normal variables. Here are a few ways to create them:
int[] numbers = new int[3];// creates an int array of 3 empty elements
int[] numbers = {10,20,30};// creates an int array of 3 elements and sets values

The power of arrays really comes to life when you use them in combination with for loops. A for loop can increment which element is being accessed/used and you can set the 'test' of the for loop to the length of the array. Every array has a variable that stores its length, the total number of elements. It can be accessed like this, arrayName.length.
int[] numbers = {10,20,30};// creates an int array of 3 elements and sets values
for(int i = 0; i < numbers.length; i++){
    println(numbers[i]);
}
// prints 10 20 30 each on their own line

Functions
Functions are a way of compartmentalizing code. Functions allow you to reuse code without having to copy and paste lines in multiple areas of your code. For example, switching back and forth from a white stroke and black fill to a black stroke and white fill.
stroke(255);
fill(0);
// to…
stroke(0);
fill(255);
Instead of having to copy and paste two lines of code each time, you could write two functions and call those commands in one line of code.
void whiteStrokeBlackFill(){
stroke(255);
fill(0);
}
// and…
void blackStrokeWhiteFill(){
stroke(0);
fill(255);
}
// to execute the functions…
whiteStrokeBlackFill();// turns the stroke to white and the fill to black
blackStrokeWhiteFill();// turns the stroke to black and the fill to white
();// turns the stroke to white and the fill to black

You can increase the usefulness of a function by setting values when you run the function. These values are called parameters. Parameters can be any data type. Here is an example:
void printNumber(int num){
println("number = " + num);
}

1 comment:

  1. I have been a herpes carrier for 2 years and I tried every possible means of curing but all of no useful until I saw a health promotion on a herbalist from West Africa who prepares herbal medicines to cure all sorts of diseases including #HSV and many others sickness but look at me today am very much happy and healthy, I'm telling you today don't lose hope keep trying God is with you, If you needs Dr.Chala help contact him on his email dr.chalaherbalhome@gmail.com or you can visit his website on http://drchalaherbalhome.godaddysites.com or https://mywa.link/dr.chalaherbalhome

    ReplyDelete