Skip to content Skip to sidebar Skip to footer

Modify A Global Variable With A Function?

In mousePressed, I need the value of aNumber, but I can't pass mousePressed(int aNumber).. so I need some sort of global to remain modified when theNumbers is called via javascript

Solution 1:

You are declaring local variable inside theNumbers() just remove int before number so your function would look like this:

int theNumbers(int aNumber) { //aNumber = 1, 2, or 3, from the javascript)println(number);
  number = aNumber; // removed int that made new local variable "number"returnnumber;
}

Solution 2:

Remove this line:

int number = aNumber;

You copy the argument to the "number" variable and then return it. In fact, you are printing global variable "number" that is not changed in the code you posted anywhere, and then return the argument, that has nothing to do with calculations you have just done.

You probably wanted to modify the global parameter number and then return it, so make a change:

int theNumbers(int aNumber) {
   // calculations for number (based on aNumber? contains number=SOMETHING somewhere?)println(number); // prints the correct number    returnnumber;
}

EDIT:

Just modify the global then. Remove the type from int number = aNumber; so you do not reinitialize the variable. In the end:

int theNumbers(int aNumber) {
   // calculations for number (based on aNumber? contains number=SOMETHING somewhere?)println(number); // prints the correct number    number = aNumber;
   returnnumber;
}

Post a Comment for "Modify A Global Variable With A Function?"