Opening Video: Tilt Brush
There will be a test at the beginning of week six’s class. This test will include one or two programming challenges and a written portion. The challenges will require a good understanding of the concepts introduced in the first five weeks of the class. The written portion will include questions related to javascript and p5. The test will be open Internet, you can consult any Internet references you wish during the test.
The example above is from last week. What happens if position is declared inside draw
?
What to Know | What to Do |
---|---|
values | operators |
literals | expressions |
variables | statements |
arrays | control structures if , for |
data objects | functions |
What to Know + What to Do |
---|
Object Oriented Programming |
A functions is a named procedure, set of instructions to preform a specific task.
There are many benefits to using functions:
The basic syntax for a function looks like this:
function name() {
// what to do
}
You have already created functions in all of your programs, setup
and draw
are functions you have defined.
Choosing good names for functions is important. Function names should concisely describe what the function does. Good function names usually start with verbs. Use camel case when naming functions and be sure the first letter is lowercase (by convention starting a function name with an uppercase letter has special meaning related to Object Oriented Programming).
In this example, the code for drawing the target has been pulled out of draw
and put into a function named drawTarget
. This makes the code easier to read, but functions can offer more that this. The function is defined starting on line 16, and called on line 12.
Parameters are values that you pass to a function when you call it. When you define the function you list the parameter names. These will work like variables (they are variables) inside your function. The values of these parameters are specified inside the parenthesis when you call the function.
function addNumbers(a, b) {
return a + b;
}
var sum = addNumbers(2, 2);
console.log(sum);
// 4
An array is an ordered set of values.
[10, 20, 30]
x = []
[]
0
, not 1
A object is a collection of named values.
{first: "Siouxsie", last: "Sioux" }
x = {}
.
(the dot operator)
This week’s homework is to recreate a series of small programs (challenges).