Serge
June 9, 2018, 7:17pm
1
https://p5js.org/examples/structure-functions.html
in this example num is use but is not defined
What is this num variable? argument not defined? else?
Here is the code:
function setup() {
createCanvas(720, 400);
background(51);
noStroke();
noLoop();
}
function draw() {
drawTarget(width*0.25, height*0.4, 200, 4);
drawTarget(width*0.5, height*0.5, 300, 10);
drawTarget(width*0.75, height*0.3, 120, 6);
}
function drawTarget(xloc, yloc, size, num) {
var grayvalues = 255/num;
var steps = size/num;
for (var i = 0; i < num; i++) {
fill(i*grayvalues);
ellipse(xloc, yloc, size - i*steps, size - i*steps);
}
}
@Serge
I’m not sure I’m understanding your post. Is there a problem with the code, or are you just wondering what type of variable “num” is? The code looks identical to the example code.
num
is the last argument passes to the drawTarget()
function:
function drawTarget(xloc, yloc, size, num) {
When this function is called, four arguments are passed in. For example, in the first call:
drawTarget(width*0.25, height*0.4, 200, 4);
The four arguments that get passed in are:
width*0.25
height*0.4
200
4
So in this case, num
will have the value of 4.
1 Like
Serge
June 9, 2018, 8:55pm
4
It seems there is no need to declare them
Thank you
Serge
June 9, 2018, 8:56pm
5
Was wondering about not declaring argument as variable
Some pedantic observations:
Parameter is a variable declared inside a function’s parentheses.
Argument is the value passed to a function’s parameter.
In JS, parameters are declared w/o any keywords; such as var
, let
or const
.
1 Like
Serge
June 9, 2018, 9:49pm
7
Thank you for the information