global local variables

edited February 2014 in Programming Questions

Hi guys I have a very basic question to ask I understand that global settings allow all variables stated there to be accessed at any level in the in the programming tree but initializing the variable in setup which also seems to be common to objects seems to do the same thing. in the simple example i wrote below is void setup() returning values of x,y to float x,y in the global area? and if so how is it doing this as i thought that void meant it cant return values ?

        float x;
        float y;

        void setup(){

          size(200,200);

          x=10;
          y=5;


        }


        void draw(){

          background(100);
          x= x+1;

          y=y+1;

          ellipse(x,y,x/2,y/2);
        }

Answers

  • You're talking about two different things here: declaring variables and initializing variables.

    To give your program access to a variable in multiple functions, or to make sure the value of a variable persists over the course of multiple function calls, you have to declare it at the class level, which is what you've done.

    It doesn't really matter where you initialize the variable, as long as the initialization happens before you use it. In your case, there isn't much difference between initializing the variables inside setup() versus initializing them in the same line as their declaration at the top of the class.

  • Most of times, we can declare and initialize variables at the same time.
    For Processing, there are some notable exceptions:

    • System variables width & height are always 0 before setup(). Don't initialize other variables or objects based on them!
    • System variable sketchPath isn't known until setup(). So don't initialize any variables or objects based on such info.
  • Cheers this becoming clearer but one question that comes up is if we initialize an object in globally at the top of the class do we still need a constructor?

  • Can you give a specific example of what you're asking about? What happened when you wrote some example code to test it out?

  • ok basically i can see why processing gives us no choice but to use a constructor when dealing with objects in a oob fashion but the code below seems to have the same functionality if i initialize the object at the top of the class or in setup i guess this is simply because we are stating that the initialized object variables hold true for our object declaration so this makes me wonder why we initialize in our variables in setup at all ?

    when would it be useful to use setup to initialize a variable and not at the top of the class or vice versa

    ** Fly f= new Fly(2,3);

    void setup(){
    
      size(200,200);
      }
    void draw(){
      background(100);
      f.move();
    
      f.display();
    }
      class Fly{
    
        float x;
    
        float y;
    
        Fly(float _x,float _y){
    
          x= _x;
          y=_y;
          }
        void move(){
    
          x =x+1;
    
          y = y+1;
    
        }
    
        void display(){
    
          ellipse(x,y,x/2,y/2);
    
        }**
    

    }

  • edited February 2014 Answer ✓

    My original point remains: you're mixing up two different things: declaring a variable and initializing a variable.

    For a variable that holds a reference to an Object (like your f variable), you declare it by giving it the type of the Object it should reference:

    //this is a declaration
    Fly f;
    

    To initialize a variable that contains a reference to an Object, you use that Object's constructor:

    //this is an initialization
    f = new Fly(2, 3);
    

    Those are two separate steps, and it doesn't really matter whether you split them up by putting the initialization inside the setup() function:

    Fly f;
    
    void setup(){
       f = new Fly(2, 3);
    }
    

    Or if you do both steps in one line:

    Fly f = new Fly(2, 3);

    Which way you choose really depends on your context. Sometimes you need to use variables that aren't initialized until the setup() function is called. For example, maybe your Fly constructor also needs to know about the width and height of the sketch. In that case, you would have to have your initialization inside the setup() function:

    //width and height aren't set yet
    Fly f;
    
    void setup(){
       //now width and height are set
       f = new Fly(2, 3, width, height);
    }
    
  • This makes total sense now thanks for that

Sign In or Register to comment.