# Function restricted variables

**URL:** <https://discourse.processing.org/t/function-restricted-variables/4104>\
**Category:** Beginners\
**Created:** [October 3, 2018, 11:54pm UTC](https://discourse.processing.org/t/function-restricted-variables/4104 "2018-10-03T23:54:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![blosh](https://avatars.discourse-cdn.com/v4/letter/b/eada6e/32.png) [@blosh](https://discourse.processing.org/u/blosh)\
**Post date:** [October 3, 2018, 11:54pm UTC](https://discourse.processing.org/t/function-restricted-variables/4104/1 "2018-10-03T23:54:44Z")

</div>

I want some variables to only exist in a certain function as I need very similar variables across multiple functions. However, I don’t want them to be reset every single time. The only other solution I can think of is to use ‘atime’, ‘btime’, ‘ctime’ etc. but this is very messy and I’m trying to clean up my code.  
So is there any way to create variables that are only recognised by one function but its values aren’t reset every time the function is called upon?

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [October 4, 2018, 2:23am UTC](https://discourse.processing.org/t/function-restricted-variables/4104/2 "2018-10-04T02:23:12Z")

</div>

I think the most elegant way would be to have global variables like `atime` and `btime`.

Can I ask you to post your functions?

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [October 4, 2018, 5:24am UTC](https://discourse.processing.org/t/function-restricted-variables/4104/3 "2018-10-04T05:24:25Z")

</div>

Hi Blosh,

It would be better to post at least an [MCVE](https://stackoverflow.com/help/mcve) so people can better understand what you want to do.

Now I think you are referring to a [static variable](https://www.geeksforgeeks.org/static-keyword-cpp/) in C++ (for example) and that can be super handy. Sadly you can’t use that in Java.

Now there is (at least) 2 ways of doing it.

The first one is kinda like your solution but using an array instead to clean up the code a bit. So instead of having 3 variable `atime`, `btime` and `ctime` you would only have `times[3]` as a variable.

Another way is to use classes. It might be overkilling but at least you have it sorted out.

```auto
myFunction1 f1;

void setup() {
  frameRate(10);
  f1 = new myFunction1();
}

void draw () {
  f1.run();
  println(f1.getTime());
}

class myFunction1 {
  private int time;
  
  myFunction1() {
    time = 0;
  }
  
  void run() {
    // Your code
    time++;
  }
  
  int getTime() {
    return time;
  }
}

```

---

<div class="post-metadata">

**Author:** ![blosh](https://avatars.discourse-cdn.com/v4/letter/b/eada6e/32.png) [@blosh](https://discourse.processing.org/u/blosh)\
**Post date:** [October 4, 2018, 6:08am UTC](https://discourse.processing.org/t/function-restricted-variables/4104/4 "2018-10-04T06:08:15Z")

</div>

Alright thanks for the feedback, will be sure to use a MCVE for future posts. Yeah I thought about using a class but I think using global variables will just turn out much easier. Thanks 🙂
