This is an old revision of the document!
p5js week 01
“Hello world” is the first program that programmers make. Let's do a drawing with text to get started.
What's a function?
A function is a tool which accepts some input, does something, and then produces some output (usually). You can think about it like a machine: you put something or things into it, it does something with those things, and then it puts out a new thing. That makes it kind of like a toaster: you put in uncooked bread, and after some time the toaster puts out toast. The program you just looked at above has 10 functions in it:
- setup
- draw
- createCanvas
- background
- fill
- circle
- ellipse
- textSize
- text
What's an argument?
Why the details matter...
JavaScript
- using the console
- using variables
- calling and making functions
- strings
- length
- order of execution
using the console
Open the console in your web browser. It's in the menu under “more tools” and then inside “developer tools”. Select the console tab.
Type the following command in the console:
console.log("hello world!")
When you type it, be sure to get every character correct. In this case, if the period, parentheses, or quotation marks are wrong, it won't work.
console.log is a function. A function is a word that does something for us. “hello world!” is a piece of data called a string. We give this data to the console.log function by putting it inside the parentheses. In this case, the data inside the parentheses is called an argument.
Now I want you try to change the argument. Try some different text (in programming, we call text “strings”) like:
- your name
- a sentence
- a whole paragraph of text!
Next, try changing the argument to a different kind of data which is not a string. A string is made by using a pair of quotation marks. This time, try using some math:
* 2 + 2 * 12 * 87 * (134/78783) + 238.000398
If you could do these things today, then you're already programming! Congratulations!
using variables
We can type 2 + 2, hit enter, and get 4. Let's store that value in a variable. We can do it like this:
a = 2 + 2
Now we can add something to the stored value like this:
a + 4
When you hit “enter”, what is the result? Why?
Think about the = operator like a tool that puts what is on the right side into a box on the left side. The box has a name. In the case above, the box is called a.
Now let's assign the value of another calculation to the variable b, like this:
b = 100 - 30
We can check the current value stored in a or b by typing that letter and pressing enter. Try that for a and b now.
Next, try the following calculation and press enter. What answer do you expect? What answer did you get? Why?
a + b
What will happen if you type this?
b - a
What about this?
b * a
Or this?
b/a
Or this?
a/b
You can use any letter or almost any word as a variable name. Try some of your own!
calling and making functions
strings
length
order of execution
p5js
- using the p5js editor
- setup
- draw
- shapes (rect, ellipse)
- coordinates
- sizes
- fill
- stroke
- color picker
sharing work
- using the wiki
- screenshots