# How to access each item from the loop via return function

**URL:** <https://discourse.processing.org/t/how-to-access-each-item-from-the-loop-via-return-function/2765>\
**Category:** Coding Questions\
**Created:** [August 18, 2018, 11:44pm UTC](https://discourse.processing.org/t/how-to-access-each-item-from-the-loop-via-return-function/2765 "2018-08-18T23:44:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Olygamy](https://avatars.discourse-cdn.com/v4/letter/o/dfb087/32.png) [@Olygamy](https://discourse.processing.org/u/Olygamy)\
**Post date:** [August 18, 2018, 11:44pm UTC](https://discourse.processing.org/t/how-to-access-each-item-from-the-loop-via-return-function/2765/1 "2018-08-18T23:44:22Z")

</div>

Hello there,

Another return function question. I have a for loop of distances between a set of xa_i and xb_i positions in the class. I need to be able to access each distance individually. I wrote the function below which prints all distances, but now in main sketch I need to access each of them individually. how do I do that?

```auto

 float finddist() {
    
   for (int i = 0; i < distance/unit+1; i++ ) {
      float newunit = distance/2 - ((unit/2)*i);
      float newPosXa = middlePos - newunit;
      float newPosXb = middlePos + newunit;
      int row = 20*i; 

      v1 = new PVector(newPosXa, posY-row);
      v2 = new PVector(newPosXb, posY-row); 
      allDist = v1.dist(v2);   
      println("distance",i,":", allDist);
    }
     return allDist;  
    }

```

Here is a whole code to explain better (basically I need to access individual length of each line (or a distance between the beginning and the end).

```auto
Middle middle;

void setup(){
  
 size(500, 500);
 background(0);
 middle = new Middle(width/2, 400, 40, 400);
   middle.makebase(); 
  middle.makelines(); 
  middle.makePoints(); 
  middle.finddist();
  
}

void draw(){
 

}

class Middle {

  float middlePos;  
  float posXa, posXb, posY;
  //float newPosXa, newPosXb;
  float distance;
  float d;
  float unit;
  PVector v1, v2;
 float allDist ;
 
  Middle(float _middlePos, float _distance, float _unit, float _posY) {
    middlePos = _middlePos;
    distance = _distance;
    unit = _unit;
    posY = _posY;
  }

  void makebase() {

    posXa = middlePos - (distance/2);
    posXb = middlePos + (distance/2);

    stroke(244, 23, 230);
    strokeWeight(0.3);
    line(posXa, posY, posXb, posY);
  }

  void makelines() {

    for (int i = 0; i < distance/unit+1; i++ ) {
      float newunit = distance/2 - ((unit/2)*i);
      float newPosXa = middlePos - newunit;
      float newPosXb = middlePos + newunit;
      int row = 20*i; 
      stroke(244, 23, 230);
      strokeWeight(0.3);
      line(newPosXa, posY-row, newPosXb, posY-row);
    }
  }

  float finddist() {
    
   for (int i = 0; i < distance/unit+1; i++ ) {
      float newunit = distance/2 - ((unit/2)*i);
      float newPosXa = middlePos - newunit;
      float newPosXb = middlePos + newunit;
      int row = 20*i; 

      v1 = new PVector(newPosXa, posY-row);
      v2 = new PVector(newPosXb, posY-row); 
      allDist = v1.dist(v2);   
      println("distance",i,":", allDist);
    }
     return allDist;  
    }
  

  void makePoints() {
    
    
    
    for (int i = 0; i < distance/unit+1; i++ ) {

      float newunit = distance/2 - ((unit/2)*i);
      float newPosXa = middlePos - newunit;
      float newPosXb = middlePos + newunit;

      int row = 20*i; 

      fill(255);
      noStroke();
      ellipse(newPosXa, posY-row, 2, 2);
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [August 19, 2018, 2:06am UTC](https://discourse.processing.org/t/how-to-access-each-item-from-the-loop-via-return-function/2765/2 "2018-08-19T02:06:47Z")

</div>

The easiest way to this is to not do it. Instead, just have a global array of values, and store the values you want to save into that array.

```auto
// At the top level:
float[] allDistances = new float[10];

// Inside your function:
allDistances[i] = whatever;

// Outside that function, but elsewhere:
if( allDistance[i] > whatever ){

```

---

<div class="post-metadata">

**Author:** ![Olygamy](https://avatars.discourse-cdn.com/v4/letter/o/dfb087/32.png) [@Olygamy](https://discourse.processing.org/u/Olygamy)\
**Post date:** [August 19, 2018, 2:53pm UTC](https://discourse.processing.org/t/how-to-access-each-item-from-the-loop-via-return-function/2765/3 "2018-08-19T14:53:12Z")

</div>

> [@TfGuy44](#):
>
> global array of values

Hi and thank you!  
However i am still a bit lost, I have tried to somehow follow your instruction. So - do you mean:

```auto

// At the top level:
float[] allDistances = new float[10];

```

Shal I place it on the of the main sketch or class? Then the function - you mean I shall skip return all together? A bit more explanaition would be ever so helpful :). And very appreciated! Thank you in advance 🙂

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [August 19, 2018, 7:46pm UTC](https://discourse.processing.org/t/how-to-access-each-item-from-the-loop-via-return-function/2765/4 "2018-08-19T19:46:31Z")

</div>

```auto

float array_of_values = new float[10];

void setup(){
  // ...
}

void draw(){
  // ...
  // You can use the values here.
  if( array_of_values[0] < 50 ){
    // ...etc
  } 
}

class YourClass {

  void yourClassFunction(){
    for( int i = 0; ... ){
      // Calculate distance here
      array_of_values[i] = the_ith_distance;
    }
    // No need to return anything.
  }
}

```
