# Return an array of values from a loop

**URL:** <https://discourse.processing.org/t/return-an-array-of-values-from-a-loop/18857>\
**Category:** Beginners\
**Created:** [March 20, 2020, 11:04pm UTC](https://discourse.processing.org/t/return-an-array-of-values-from-a-loop/18857 "2020-03-20T23:04:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrbro](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mrbro/32/8485_2.png) [@mrbro](https://discourse.processing.org/u/mrbro)\
**Post date:** [March 20, 2020, 11:04pm UTC](https://discourse.processing.org/t/return-an-array-of-values-from-a-loop/18857/1 "2020-03-20T23:04:22Z")

</div>

Hi there!

I just started using processing, and I am very new to coding.

I am struggling with scope or arrays, im not even sure.

I create an array of points, and paint them by using Perlin Noise.  
However, I want to use “stroke” outside of the “for loop”.  
Can anyone help me?

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

Including the code:

```auto
float inc = 0.1; // SIZE oF NOISE
float scl = 10; // SCALE of GRID
float r;

void setup() {
  size(500,500);
  noSmooth(); // Draws all geometry and fonts with jagged edges
  strokeCap(PROJECT); // appearance of the points: ROUND - circle, PROJECT - square
  strokeWeight(5); // thicknes of the points
  
}

void draw() {
  float yoff = 0;
  for (float y = 0; y < height; y++) {
    float xoff = 0;
    for (float x = 0; x < width; x++) {
      float r = noise(xoff, yoff) * 255;
      xoff += inc;
      //stroke(r);
      point(x * scl, y * scl);
    }
    yoff += inc;
  }
  stroke(r); // <----------- I need it here for further development
  println(r);
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [March 21, 2020, 1:38am UTC](https://discourse.processing.org/t/return-an-array-of-values-from-a-loop/18857/2 "2020-03-21T01:38:20Z")

</div>

Hello,

```auto
for (float x = 0; x < width; x++) {
      r = noise(xoff, yoff) * 255; // <----- Remove the float
      xoff += inc;
      //stroke(r);
      point(x * scl, y * scl);
    }

```

Explained here:  
[https://processing.org/examples/variablescope.html](https://processing.org/examples/variablescope.html)

---

<div class="post-metadata">

**Author:** ![mrbro](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mrbro/32/8485_2.png) [@mrbro](https://discourse.processing.org/u/mrbro)\
**Post date:** [March 21, 2020, 2:24am UTC](https://discourse.processing.org/t/return-an-array-of-values-from-a-loop/18857/3 "2020-03-21T02:24:56Z")

</div>

Hi glv, thank you for your response!

When I do like so, it renders the data with only one value ☹

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/340646f4cbeec016a158a97a1ec9ce80a545d5c5.png)

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [March 21, 2020, 2:27am UTC](https://discourse.processing.org/t/return-an-array-of-values-from-a-loop/18857/4 "2020-03-21T02:27:41Z")

</div>

Hi,

Go through your code and address all the scope issues; there may be other logical errors as well.  
I showed you one obvious example.  
There may be others to sort out.

I just took one quick look… I will leave this with you to sort out.

---

<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:** [March 21, 2020, 9:42am UTC](https://discourse.processing.org/t/return-an-array-of-values-from-a-loop/18857/5 "2020-03-21T09:42:55Z")

</div>

> [@mrbro](#):
>
> However, I want to use “stroke” outside of the “for loop”.  
> Can anyone help me?

You can store ALL colors from r into an array (1D or 2D array, see tutorials: [Tutorials / Processing.org](https://www.processing.org/tutorials/)).

In my code below the array is listColors2. (listColors is only temporary)

**The scope**

> [@mrbro](#):
>
> …r… // \<----------- I need it here for further development

The scope of r is only INSIDE the for-loop because of this line:

```
  float r = noise(xoff, yoff) * 255;

```

you can use r after the for loop but then you would use only the last value of r (not all of them like in an array). Would that be enough for you?

To achieve this say `float r;` **before** the for loop (or even before setup()) and say `r = noise(xoff, yoff) * 255;` within the for-loop (without repeating the word “float”).

- before the for loop means: scope = function
- before setup() means: scope = entire sketch
- inside the for-loop: scope = inside the for-loop

Explained here:

> **[Variable Scope / Examples](https://processing.org/examples/variablescope.html)**
>
> Variables have a global or local "scope". For example, variables declared within either the setup() or draw() functions may be only used in these functions. Global variables, variables declared outsid…

**The colors**

By the way, data type `color` is in fact `int`.

> **[color / Reference](https://www.processing.org/reference/color_datatype.html)**
>
> Datatype for storing color values. Colors may be assigned with get() and color() or they may be specified directly using hexadecimal notation such as #FFCC00 or 0xFFFFCC00.…

> **[Color](https://www.processing.org/tutorials/color/)**
>
> An introduction to digital color.

so you can say

int r = 23344;

- stroke(r); - for point or line
- fill(r); - for ellipses, rect, cube, sphere (for sphere() together with noStroke();)…

or

- color(44); // gray or
- color(255,2,2); // which is red.

Chrisir

**Full code with array**

```auto
float inc = 0.1; // SIZE of NOISE
float scl = 10; // SCALE of GRID
float r;

color[] listColors2; 

void setup() {
  size(500, 500);
  noSmooth(); // Draws all geometry and fonts with jagged edges
  strokeCap(PROJECT); // appearance of the points: ROUND - circle, PROJECT - square
  strokeWeight(5); // thickness of the points
}//

void draw() {

  IntList listColors = new IntList();  

  float yoff = 0;
  for (float y = 0; y < height; y++) {
    float xoff = 0;
    for (float x = 0; x < width; x++) {
      float r = noise(xoff, yoff) * 255;
      xoff += inc;
      stroke(r);
      listColors.append(int(r)); 
      point(x * scl, y * scl);
    }
    yoff += inc;
  }
  // stroke(r); // <----------- I need it here for further development
  listColors2 = listColors.array();

  for (color c : listColors2) {
    println(c);
  }
  //
}//
//

```
