Where to declare/initialize

Hi, I've just started processing. I can't understand where to declare and where to initialize variables. Here is a specific example of what I'm asking about. If global variables were effective over the whole program tree, why can't I declare and initialize "float d" at the same time as a global variable? (just like int x=120, int y=60, int radius=12) Processing_Question I'm not a native English speaker, so if my question is not clear, please let me know.

Answers

  • Answer ✓

    On the left, you have a local variable that is created new every time draw runs. You can not access the value of d outside of draw, and once draw ends, the variable is gone.

    In the middle, you have a global variable, In draw, you assign this global variable a value, which changes as the mouse moves, and it updated every time draw runs.

    On the right, you have a global variable again, which stores a single value. This time, however, the value is never updated once it is set! The variable is not "re-computing" the expression you've "assigned" to it - you assign it a value, not an expression. This isn't what you want.

    If you want to use d in other functions, go with the middle.

    If you don't, go with the left. Or, forget about using d entirely, and compare dist(mouseX,mouseY,x,y) with radius directly:

    if ( dist(mouseX,mouseY,,y)<radius ) {
    
  • Answer ✓

    In 3rd picture the other variables are constants, so their value is known by the compiler when it sees them. Line 4, however, is calling a method to set the value so the value isn't known, isn't constant.

    • Any variable declared outside any curly braces is called a field, a.K.a. "global".
    • Otherwise it is local to the curly braces' scope.
    • If it happens to have the same name as a field, the latter is temporarily overshadowed by the former within that scope.
  • Answer ✓

    In the first picture, d isn't global, it is scoped to draw () and only available within draw ().

    In the second picture, d does have global scope and is available everywhere.

    In general it's a good idea to limit a variable's scope as much as possible, keeps things neat.

  • edited September 2015 Answer ✓

    the line

    d = dist(mouseX...............

    must be in draw()

    it is not enough to call it once;

    you must call it again and again (this is what draw() does).

    Remark

    so d = dist(mouseX............... is

    • a one time command you have to call again and again

    • it is not a standing order or a rule that the sketch would automatically do forever once you said it

    as has been said.

    ;-)

  • ah, beaten to it by TfGuy44. in my defence it was 5:25AM where i was 8)

  • Thank you all. So, commands in the global field are processed only one time and I can't assign variables to variables in the field.(not sure this sentence makes sense) I think the problem is solved! :)

  • edited September 2015 Answer ✓

    well 1st part is correct (commands only once)

    2nd part is wrong (about the assignement)

    ;-)

  • edited September 2015 Answer ✓

    besides, nobody says field vars (except gotoloop), they are global vars.

  • Oh, the second part is wrong!? I'll go over again. Thanks ;;)

  • Answer ✓

    As GoToLoop pointed out

    Any variable declared outside any curly braces is called a field, a.K.a. "global".

    This perfectly true in Processing they are generally called global variables. in Java they are called fields or attributes.

    Since the Processing language is based on Java then all 3 terms describe the same thing.

  • Thank you, quark. I had totally mixed up those expressions.

Sign In or Register to comment.