[SOLVED] How to use variables from one function in another, when variables were ...

I am not very skilled in Processing, but I have been trying to convert my sketch from mouse clicks to using Kinect hand states. How do I use variables from function1 in function2, when the variables were declared using function1's arguments?

void shapes(int posx, int posy)
{
 pushMatrix();
translate(width/3,200);
 for (int i=0; i<cols; i++) {
    for (int j=0; j<rows; j++) {

      int shapeIndex = shapeNumbers[i][j];
      PShape currentShape = shapes[shapeIndex];
     currentShape.setFill(colors[i][j]);
     currentShape.setStroke(false);
     shape(currentShape,i*posx,j*posy,shapesize,shapesize);

    if (mousePressed ==true){
    int  x = i*posx+width/3;
     int y = j*posy+200;
     if (mouseX > x && mouseX < (x + shapesize) && mouseY > y && mouseY < (y + shapesize)){
      colors[i][j]=color(255);
    }
    }
    }
  }
    popMatrix();
}

void shapes(int posx, int posy){
 pushMatrix();
  translate(width/3,200);
 for (int i=0; i<cols; i++) {
    for (int j=0; j<rows; j++) {

      int shapeIndex = shapeNumbers[i][j];
PShape currentShape = shapes[shapeIndex];
     currentShape.setFill(colors[i][j]);
     currentShape.setStroke(false);
    shape(currentShape,i*posx,j*posy,shapesize,shapesize);

    if (mousePressed ==true){
    int  x = i*posx+width/3;
     int y = j*posy+200;
     if (mouseX > x && mouseX < (x + shapesize) && mouseY > y && mouseY < (y + shapesize)){
      colors[i][j]=color(255);
    }
    }
    }
  }
    popMatrix();
}

Answers

  • Not sure why it is formatting wierd...

  • edited April 2018

    Edit your post (gear on top right side of any of your posts), select your code and hit ctrl+o. Leave an empty line above and below your block of code. Details here: https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text

    ****EDIT: What functions are you referring to?

    Kf

  • Answer ✓

    Thank you! I ended up finding out the answer

  • (fixed formatting, didn't fix terrible indentation. How do people live like that?)

  • @borgejor -- ust Ctrl-T in Processing IDE to auto-format code. This will make it easier to read.

  • edited April 2018 Answer ✓

    Did you post the same function twice?

    How do I use variables from function1 in function2, when the variables were declared using function1's arguments?

    Several ways: instead of having local variables in function1 just use global variables. They must be unique and meaningful so instead of x maybe xInitialUserInput etc.

    When you set these variables in a function they are changed globally and you can use them in the other function too

    Example: function with a global variable b:

    int b;
    
    void setup() {
      size(565, 556);
    
      println (b); 
      testFunction(); 
      println (b);
    }
    
    void testFunction() {
      b=b+5;
    }
    //
    
  • edited April 2018

    Another way is that a function can return a variable like

    a=add(...);
    
    ....
    
    int add(...) {  // this would be the function, see below
        ....
    }
    

    Example for local variable in connection with a function that returns a value:

    void setup() {
      size(565, 556);
    
      int b=0;
    
      println (b); 
      b=testFunction(b); 
      println (b);
    }
    
    int testFunction(int inputInt) {
      int temp=inputInt+5;
      return temp;
    }
    //
    

    Remark:

    you can also return an object (useful when you need to return multiple values at once) or an array or an array of objects from the function.

    What doesn't work

    What doesn't work is to change the variable (passed to the function as a parameter) in the function. It's not changing the value of the variable in setup (in the calling function. (since we have a call by value imho)

    void setup() {
      size(565, 556);
    
      int b=0;
    
      println (b);  // prints 0
      testFunction(b); 
      println (b);  // still prints 0
    }
    
    void testFunction(int inputInt) {
      inputInt=inputInt+5;
      println(inputInt);  // prints 5
    }
    //
    

    see also

    see also

    https://github.com/Kango/Processing-snippets/wiki/programming-and-functions

Sign In or Register to comment.