Passing an Argument to a Function

Hi -
I’m new to Processing and I’m trying to make a simple function that allows me to make grids out of a variably-input shapes.

I am not exactly sure how this would work - I still don’t understand a lot of the terminology. I am sharing some code below that will maybe show what I’m trying to do, even though it doesn’t work properly.

import processing.svg.*;

void setup() {
  size(800, 800);
  noLoop();   
}

void draw() {
   gridMaker(arc(50, 50, 80, 80, 0, PI));
        }
      }
  };
  arc
  
 void gridMaker() { 
  shape
   for (int u = 1; u<20; u++){
     for (int v = 1; v<20; v++){
        pushMatrix();
        translate(u * 10, v*10);
        {shape}
         popMatrix();
     }
   }
 };
1 Like

Hi @bcabc,

Welcome to the forum! :wink:

If I paste the code in the Processing editor, I get the following errors:

image

As you can see, your program is not well structured and therefore the computer can’t run your code.

Programming languages always force you to write code with a certain syntax and format. For example blocks (opened with a curly bracket) are always closed.

If you are a beginner with Processing, I encourage you to check the tutorials:

2 Likes

Hi Josephh -
Yes, I understand that the code does not work. What I was hoping is that someone here could help me get it to work. The idea is that I would be able to pass a “shape” - something like an arc - to the gridMaker function. Is that possible? Or does it need to be written totally differently?

All right but is it your first coding experience? Because the way you wrote the code is not correct in Java (the language used by Processing).

For example, every instruction must end with a semi colon, so writing arc and shape doesn’t work.

Also, the arc() function doesn’t return anything (see the doc), it returns void. And a variable can’t have a void type in Java.

You have additional curly brackets that doesn’t close a block (after gridMaker) and therefore it can’t compile.

What you want to do can be achieved with enumerations (passing an enum as argument and switching on it in the for loop to draw the correct shape), classes that inherit from a Shape class and have a draw method so you can pass any instance of a shape to your grid, passing a PShape object to your function…

Hello and welcome to the forum!

You can read more about functions here: programming and functions · Kango/Processing-snippets Wiki · GitHub

2 Likes

Your idea to pass a function as an argument is possible – and a good idea – but extremely awkward in Processing 3’s flavor of Java. If you want to pass functions as arguments, try Processing python mode – in Python it is dead simple, and works more or less the way that you imagined.

If you want to use Processing (original Java mode) to make a grid of custom shapes, the easiest way is to use PShape. Define a grid method that takes a PShape object as an argument

void gridMaker(PShape ps)

It will loop your grid … then draw the shape on the inner loop:

shape(ps, x, y);

To pass your argument to your grid, create a shape, then pass it:

PShape myshape = createShape(ARC, 0, 0, 50, 50, 0, HALF_PI);
gridMaker(myshape);

You can create several shapes and pass them based on if statements – time, keys pressed, etc.

4 Likes

Thanks, Jeremy!
One small follow-up: it seems that PShape doesn’t play very nice with “SVG” mode - do you happen to know if the Python method would work better for that purpose?

There are many options, so it would help to have a better idea if what you are trying to accomplish and what specific problem you are having. Two options:

  • Use loadShape if you want grids of SVG objects.
  • Capture drawing elements like strokes using PGraphics.

There is a simple example of using PGraphics at the end of the SVG library reference:

2 Likes