# Assigning function to a variable

**URL:** <https://discourse.processing.org/t/assigning-function-to-a-variable/28563>\
**Category:** Coding Questions\
**Created:** [March 16, 2021, 2:40am UTC](https://discourse.processing.org/t/assigning-function-to-a-variable/28563 "2021-03-16T02:40:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mattyjak](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mattyjak/32/16777_2.png) [@Mattyjak](https://discourse.processing.org/u/Mattyjak)\
**Post date:** [March 16, 2021, 2:40am UTC](https://discourse.processing.org/t/assigning-function-to-a-variable/28563/1 "2021-03-16T02:40:09Z")

</div>

Hello everybody! I’m a beginner at processing (but I have some experience with p5js and PJs).  
I’m trying to find a way to assign a function to a variable. Essentially, I want to be able to set a function to a separate variable, and whenever that function is called, it calls my new function (which was defined by that variable)

Here’s the code I’m working with:

```auto
class button{
 float posx,posy,widt,heig;
 int buttonColor = color(0,0,0);
 boolean customFunc; //I've tried void, int, and bool. Nothing wants to work.
 button(float x, float y, float wid, float hei, int bColor, boolean func){
  posx = x;
  posy = y;
  widt = wid;
  heig = hei;
  buttonColor = bColor;
  customFunc = func; //HERE IS ANOTHER THINGY 
 }
 
 void clickDet(){
  if(mouseX >= posx && mouseX <= posx+widt && mouseY >= posy && mouseY <= posy+heig){
    if(mouseButton1Click==1){
     println("CLICKED ME!");
   //HERE
    }
  }
 }

```

I can’t find a way to do this easily. I know I could use an array/list, but that wouldn’t be very efficient in the long run. I also don’t want to really assign each button a custom ID (but I probably will anyway for debugging).

I’ve made code for buttons using the JavaScript ports, but I can’t get anything to work here. Please help me out here guys!  
EDIT: Here’s the code for my JavaScript buttons:

```auto
var button = function(config){
    this.x = config.x || 25;
    this.y = config.y || 25;
    this.w = config.w || 40;
    this.h = config.h || 25;
    this.textblack = config.tb || false;
    this.textlabel = config.label || "button";
    this.activate = config.activate || function(){}; //THIS IS WHAT I'M TRYING TO GET TO WORK IN PROCESSING
    this.chosenfont = config.tfont || "arial";
    this.textsiz = config.tsize || 10;
    
};
button.prototype.clickdet = function(){
    return mouseX >= this.x && mouseX <= (this.x + this.w) && mouseY >= this.y && mouseY <= (this.y + this.h) && mouse.clicked.m1; 
};
button.prototype.draw = function() {
     if(this.clickdet()){
         buttonlog.push("PRESSED BUTTON" + this.textlabel);
         this.activate(); //THIS IS WHAT I WANT TO DO IN PROCESSING 
     }
     rect(this.x,this.y,this.w,this.h);
     if(this.textblack === true){
      fill(0);
     } else{fill(255);}
     textFont(this.chosenfont,this.textsiz);
     text(this.textlabel,this.x+this.w/6,this.y+this.h/2.5);
}; 

```

---

<div class="post-metadata">

**Author:** ![Swiss](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Swiss](https://discourse.processing.org/u/Swiss)\
**Post date:** [March 16, 2021, 4:58am UTC](https://discourse.processing.org/t/assigning-function-to-a-variable/28563/2 "2021-03-16T04:58:25Z")

</div>

Hi Mattyjak,

Sounds like maybe you want a [supplier](http://www.java2s.com/Tutorials/Java/java.util.function/Supplier/index.htm)?

What is the function that you are passing in and where are you calling it?

---

<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 16, 2021, 11:05am UTC](https://discourse.processing.org/t/assigning-function-to-a-variable/28563/3 "2021-03-16T11:05:51Z")

</div>

> [@Mattyjak](#):
>
> I want to be able to set a function to a separate variable,

Consider switch\case:

- [switch / Reference / Processing.org](https://processing.org/reference/switch.html)
- [case / Reference / Processing.org](https://processing.org/reference/case.html)

`:)`

---

<div class="post-metadata">

**Author:** ![Mattyjak](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mattyjak/32/16777_2.png) [@Mattyjak](https://discourse.processing.org/u/Mattyjak)\
**Post date:** [March 16, 2021, 6:09pm UTC](https://discourse.processing.org/t/assigning-function-to-a-variable/28563/4 "2021-03-16T18:09:14Z")

</div>

Not quite what I’m looking for, although this could be useful. I’m trying to pass a function that will be changed, as I am creating a button object/class to be used throughout my project.

In essence I’m trying to create a button object that can be called like this:

```auto
button startButton = new button(x,y,width,height,color,function);

```

and when clicked, it runs the ‘function’ argument. (In essence, do the javascript thing of storing a function inside of a variable)

---

<div class="post-metadata">

**Author:** ![Mattyjak](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mattyjak/32/16777_2.png) [@Mattyjak](https://discourse.processing.org/u/Mattyjak)\
**Post date:** [March 16, 2021, 6:11pm UTC](https://discourse.processing.org/t/assigning-function-to-a-variable/28563/5 "2021-03-16T18:11:05Z")

</div>

I know that I could use that as my method for running the functions inside of each button object that I create, but it seems like **a lot of extra work** and I want to optimize it.

As opposed to writing the function to a switch statement, then assigning an ID to the button, I want to be able to assign a function to the button using the built-in argument passer.

Basically I want to be able to use a function as a variable. (And one that does not return anything {at least usually})

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [March 16, 2021, 6:30pm UTC](https://discourse.processing.org/t/assigning-function-to-a-variable/28563/6 "2021-03-16T18:30:27Z")

</div>

I guess it’s not the java way to do it and you need to extend a class ☹

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/4506/how-to-make-a-function-that-has-a-callback)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

although the names are similar, how you code in java and javascript is quite different especially around types.

---

<div class="post-metadata">

**Author:** ![Mattyjak](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mattyjak/32/16777_2.png) [@Mattyjak](https://discourse.processing.org/u/Mattyjak)\
**Post date:** [March 16, 2021, 7:12pm UTC](https://discourse.processing.org/t/assigning-function-to-a-variable/28563/7 "2021-03-16T19:12:52Z")

</div>

OK, I found a way that works, but I didn’t want to use it. I take advantage of the switch statement’s speed at finding addresses inside of an index. By using this, I can call it each frame without (hopefully) affected performance too badly.

```auto
int buttonFunctionIdManager = 0;
void button_Management(){
  switch(buttonFunctionIdManager){
   case 0: //DO NOTHIGN
   break;
   case 1: //DO SOMETHING NOT RESERVED
         print("WE FIRED IT");
         bFix();
   break;
   
   
   default: 
           println("[13]ERROR SOMEONE DID A BAD FUNCTION CALL: "+str(buttonFunctionIdManager));
           bFix();
   break;
  }
}
button startGameButton = new button(25,25,25,25,3,color(250,0,255),color(0,255,255),"EYOOO");

```

I would have preferred to just assign a function to each button in the constructor, but yeah I can’t seem to figure that out.  
This will require me to assign each function into the switch statement and to add the bFix(); function to reset it after every call. So in others words, more work is needed for the same amount of reward.

---

<div class="post-metadata">

**Author:** ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)\
**Post date:** [March 16, 2021, 8:59pm UTC](https://discourse.processing.org/t/assigning-function-to-a-variable/28563/8 "2021-03-16T20:59:16Z")

</div>

This is called a **delegate** : delegates are used to pass methods as arguments to other methods. Java doesn’t support this.

* * *

What you can do instead is use an **interface** ; assign the button an **instance** of the interface, then call the methods against the interface.

For example, the _ControlP5_ library supports adding a `callBackListener` to a UI element. When the UI element is clicked/changes value, it calls the relevant method implemented in the user’s callback listener. Perhaps you’re looking to do something similar (call _some_ method when the button is pressed for example).

```auto
myButton.addCallback(new CallbackListener() {
					@Override
					public void controlEvent(CallbackEvent input) {
                         myCode()...
					}
				});

```

---

<div class="post-metadata">

**Author:** ![Mattyjak](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mattyjak/32/16777_2.png) [@Mattyjak](https://discourse.processing.org/u/Mattyjak)\
**Post date:** [March 17, 2021, 1:13am UTC](https://discourse.processing.org/t/assigning-function-to-a-variable/28563/9 "2021-03-17T01:13:13Z")

</div>

I’ll try and see if I can implement that instead of my really bad switch method. Thank you for the help!
