Num undefined, no messages

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.

  • Trilobyte

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:

  1. width*0.25
  2. height*0.4
  3. 200
  4. 4

So in this case, num will have the value of 4.

1 Like

It seems there is no need to declare them

Thank you

Was wondering about not declaring argument as variable

Some pedantic observations: :face_with_monocle:

  1. Parameter is a variable declared inside a function’s parentheses.
  2. Argument is the value passed to a function’s parameter.
  3. In JS, parameters are declared w/o any keywords; such as var, let or const.
1 Like

Thank you for the information