# Loop with function

**URL:** https://discourse.processing.org/t/loop-with-function/37261
**Category:** Coding Questions
**Created:** [June 1, 2022, 12:06am UTC](https://discourse.processing.org/t/loop-with-function/37261 "2022-06-01T00:06:47Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![humano](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@humano](https://discourse.processing.org/u/humano)
#### Post date: [June 1, 2022, 12:06am UTC](https://discourse.processing.org/t/loop-with-function/37261/1 "2022-06-01T00:06:47Z")

</div>

Howdy! I am trying to do a loop with these lines that I have created on function “brush” but I only can draw it once. Could you please tell me how repeat this line with a loop?

 ![scr](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/f/9/f93e05cb0b2108000da3f80c5fc6d438d3f4fe49.jpeg)

```auto
void setup(){
size(1000,1000);
background(255,255,0);
noStroke();
}

void draw(){
// for (int j =0; j<1000; j++) {
  brush(0,0);
}
//}

void brush(int x, int y){
strokeWeight(20);
colorMode(HSB, 360,100,100,1000);
for (int i = 0; i < 1000; i++) {
    stroke(360,100,100,800-i);
    point(i,0);
  }
  noLoop();
} 

```

---

<div class="post-metadata">

### Author: ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)
#### Post date: [June 1, 2022, 7:10am UTC](https://discourse.processing.org/t/loop-with-function/37261/2 "2022-06-01T07:10:52Z")

</div>

Hi @humano,

what about this approach …

```auto
void draw(){
  background(255,255,0);
  strokeWeight(20);
  colorMode(HSB, 360,100,100,width);

  for (int j=1; j <= 10; j++) {
    brush(0,j*25,width/j);
  }
}

void brush(int x, int y, int len){
for (int i = 0; i < len; i++) {
    stroke(360,100,100,(len*0.8)-i);
    point(i,y);
  }
} 

```

Cheers  
— mnse

![dummy4](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/d/f/df14e9fbf2cdff9d5c037c94bbc1b39a368eba0a.png)

---

<div class="post-metadata">

### Author: ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)
#### Post date: [June 1, 2022, 7:18am UTC](https://discourse.processing.org/t/loop-with-function/37261/3 "2022-06-01T07:18:45Z")

</div>

Or slightly modified to encapsulate and use also x coordinate…

```auto
void draw(){
  background(255,255,0);
  for (int j=1; j <= 10; j++) {
    brush(j*5,j*25,width/j);
  }
}

void brush(int x, int y, int len){
  pushStyle();
  colorMode(HSB, 360,100,100,len);
  strokeWeight(20);  
  for (int i = 0; i < len; i++) {
    stroke(360,100,100,len-i);
    point(x+i,y);
  }
  popStyle();
} 

```

Cheers  
— mnse

![dummy5](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/6/5/65917500440fd5f8885b3612e498e898067b8ada.png)

---

<div class="post-metadata">

### Author: ![humano](https://avatars.discourse-cdn.com/v4/letter/h/dbc845/32.png) [@humano](https://discourse.processing.org/u/humano)
#### Post date: [June 2, 2022, 5:13pm UTC](https://discourse.processing.org/t/loop-with-function/37261/4 "2022-06-02T17:13:16Z")

</div>

thank you so much, awesome!
