xxxxxxxxxx
44
/*
From MDN:
"Functions are one of the fundamental building blocks in JavaScript. A function in JavaScript is similar to a procedure—a set of statements that performs a task or calculates a value, but for a procedure to qualify as a function, it should take some input and return an output where there is some obvious relationship between the input and the output. To use a function, you must define it somewhere in the scope from which you wish to call it."
- Defining functions or function declaration or function statement
- function keyword
- parameters within parenthesis
- statement within {}
- Example
function double(number){
return number * 2;
}
- The function double takes one parameter (number) and has one statement that returns the provided number multiplied by 2.
- Calling functions
- Example double(10);
- Funcion declarations do not execute the function. It needs to be called
- Parameters vs Arguments.
- Parameters are placeholders in functions or 'variables'
- Arguments are the actual passed information
- In JavaScript you can pass functions to functions, meaning that a function can be an Argument.
*/
function setup() {
createCanvas(400, 400);
textSize(40);
}
function draw() {
fill(255);
text(double(4), width/2, height/2);
if(logIt() % 20 === 0){
background(220);
text(logIt(), 200, 100);
}
}
function double(num){
return num * 2;
}
function logIt(){
return floor(frameRate());
}