# Looping counter inside function

**URL:** <https://discourse.processing.org/t/looping-counter-inside-function/4763>\
**Category:** Coding Questions\
**Created:** [October 22, 2018, 8:38pm UTC](https://discourse.processing.org/t/looping-counter-inside-function/4763 "2018-10-22T20:38:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![spade](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/spade/32/2037_2.png) [@spade](https://discourse.processing.org/u/spade)\
**Post date:** [October 22, 2018, 8:38pm UTC](https://discourse.processing.org/t/looping-counter-inside-function/4763/1 "2018-10-22T20:38:07Z")

</div>

Hi, I was trying to initiate a variable inside a function that saves a time reference so I know how much time has passed since it was used (to sync with the BPM). This is my function:

```auto
void rainbow(int bpm){
  
  float bpmt = 60000/bpm; //calcula el tiempo de cada beat
  float xt = t-rt2;
  float speed = map(xt,0,bpmt,0,255);
  if(xt>bpmt){rt2=t-int(xt-bpmt);}//calcula el fin y compensa la sobra de bpm
  int hue = int(speed);
  fill(wheel(hue));
  rect(0,0,SIZE_X,SIZE_Y);

}

```

`rt2` here is a global variable, but I need it to be a variable inside the function. What I don´t know is how to initiate `rt2` (as `rt`) inside the function without it being reinitiated in every loop.

`t` is the value of `millis()`  
`xt` is the time that has passed since `rt` was recorded  
`bpmt` is the duration of a beat  
`rt` is the reference time at which a beat “started”

Maybe I’m thinking it wrong, but basically what I want to do is use the passing of time inside a beat to do some actions accordingly. The problem is that I need to do this in various functions that run on the same frame, so I can’t use a global variable and I don’t want to create “placeholder” global variables either (that’s what I’m doing now).

I share below another function that uses the value of `xt` for some operations.

```auto
void senoMask(int bpm){
  
  float a = 0.0;//para dibujar seno
  float b = 0.0;//para modificar la altura
  float inc = TWO_PI/SIZE_X;//incremento vertical entre linea y linea
  
  long xt = t-rt1;
  int bpmt = 60000/bpm*2; //calcula el tiempo de cada beat
  float incb = TWO_PI/bpmt*xt; //calcula el incremento en relación a los BPM
  
  for (int i = -1; i < SIZE_X; i++) {
    float y = cos(b)*SIZE_Y;
    float z = cos(a)*y;//alto de la linea que se dibuja
    if(z>0){z=0;}//se asegura de que solo se muestre la altura cuando es positiva
    stroke(1);
    line(i, 0, i, SIZE_Y+z);//dibuja cada linea
    a += inc; //aplica el incremento en a
    b = incb; //aplica el incremento en b
  }
  if(xt>bpmt){rt1=t-(xt-bpmt);}

}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [October 23, 2018, 12:21am UTC](https://discourse.processing.org/t/looping-counter-inside-function/4763/2 "2018-10-23T00:21:22Z")

</div>

Java don’t have static local variables like C. Few languages got such feature AFAIK: 😮

> **[Local variable | Static local variables](https://en.wikipedia.org/wiki/Local_variable#Static_local_variables)**
>
> A special type of local variable, called a static local, is available in many mainstream languages (including C/C++, Visual Basic, and VB.NET) which allows a value to be retained from one call of the function to another – it is a static variable with local scope. In this case, recursive calls to the function also have access to the (single, statically allocated) variable. In all of the above languages, static variables are declared as such with a special storage class keyword (e.g., static).
> Sta...

In order to retain stored values inside variables under Java, they have to be declared as fields. 😐

So you’re gonna have to wrap your function inside a class in order to have fields under its control. 🎁

---

<div class="post-metadata">

**Author:** ![spade](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/spade/32/2037_2.png) [@spade](https://discourse.processing.org/u/spade)\
**Post date:** [October 23, 2018, 3:31am UTC](https://discourse.processing.org/t/looping-counter-inside-function/4763/3 "2018-10-23T03:31:59Z")

</div>

Hi, thank you. I wasn’t much into Classes yet, I’m a little now (not so much a coding expert). It works like I want now. 🤣👍

---

<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:** [October 24, 2018, 6:16pm UTC](https://discourse.processing.org/t/looping-counter-inside-function/4763/4 "2018-10-24T18:16:56Z")

</div>

@spade In many basic Processing example sketches that demonstrate this kind of thing before introducing classes, they simply declare a global variable for the counter at the top of the sketch.

A helpful example sketch for understanding this is Variable Scope:

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

…that global variable actually does become a class variable eventually – it is a variable inside the Processing sketch class, which is not visible to the Processing programmer in the PDE editor before the sketch is compiled and run.
