# Passing an Argument to a Function

**URL:** <https://discourse.processing.org/t/passing-an-argument-to-a-function/31933>\
**Category:** Beginners\
**Created:** [August 26, 2021, 3:01am UTC](https://discourse.processing.org/t/passing-an-argument-to-a-function/31933 "2021-08-26T03:01:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bcabc](https://avatars.discourse-cdn.com/v4/letter/b/6a8cbe/32.png) [@bcabc](https://discourse.processing.org/u/bcabc)\
**Post date:** [August 26, 2021, 3:01am UTC](https://discourse.processing.org/t/passing-an-argument-to-a-function/31933/1 "2021-08-26T03:01:55Z")

</div>

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.

```auto
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();
     }
   }
 };

```

```auto

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [August 26, 2021, 8:41am UTC](https://discourse.processing.org/t/passing-an-argument-to-a-function/31933/2 "2021-08-26T08:41:23Z")

</div>

Hi @bcabc,

Welcome to the forum! 😉

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

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/93107f8afac6e4786c51047c61b59123bec68753.png)

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:

> **[Tutorials](https://processing.org/tutorials/)**
>
> Links to videos that cover the Processing basics.

---

<div class="post-metadata">

**Author:** ![bcabc](https://avatars.discourse-cdn.com/v4/letter/b/6a8cbe/32.png) [@bcabc](https://discourse.processing.org/u/bcabc)\
**Post date:** [August 27, 2021, 12:38am UTC](https://discourse.processing.org/t/passing-an-argument-to-a-function/31933/3 "2021-08-27T00:38:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [August 27, 2021, 8:30am UTC](https://discourse.processing.org/t/passing-an-argument-to-a-function/31933/4 "2021-08-27T08:30:17Z")

</div>

> [@bcabc](#):
>
> Yes, I understand that the code does not work. What I was hoping is that someone here could help me get it to work.

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](https://processing.org/reference/arc_.html)), 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`](https://processing.org/reference/PShape.html) object to your function…

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [August 27, 2021, 12:00pm UTC](https://discourse.processing.org/t/passing-an-argument-to-a-function/31933/5 "2021-08-27T12:00:40Z")

</div>

Hello and welcome to the forum!

You can read more about functions here: [programming and functions · Kango/Processing-snippets Wiki · GitHub](https://github.com/Kango/Processing-snippets/wiki/programming-and-functions)

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [August 27, 2021, 3:41pm UTC](https://discourse.processing.org/t/passing-an-argument-to-a-function/31933/6 "2021-08-27T15:41:58Z")

</div>

> [@bcabc](#):
>
> I’m trying to make a simple function that allows me to make grids out of a variably-input shapes.

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:

```auto
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.

- [createShape() / Reference / Processing.org](https://processing.org/reference/createShape_.html)
- [arc() / Reference / Processing.org](https://processing.org/reference/arc_.html)

---

<div class="post-metadata">

**Author:** ![bcabc](https://avatars.discourse-cdn.com/v4/letter/b/6a8cbe/32.png) [@bcabc](https://discourse.processing.org/u/bcabc)\
**Post date:** [August 28, 2021, 12:17am UTC](https://discourse.processing.org/t/passing-an-argument-to-a-function/31933/7 "2021-08-28T00:17:12Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [August 28, 2021, 2:39pm UTC](https://discourse.processing.org/t/passing-an-argument-to-a-function/31933/8 "2021-08-28T14:39:12Z")

</div>

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:

> **[SVG Export / Libraries](https://processing.org/reference/libraries/svg/index.html)**
>
> Create SVG files.
