Returning an array?

I have this code:

int[] colors(String shapeShade){ if(shapeShade == "red"){ int[] colorArray = {255,0,0}; } else if(shapeShade == "blue"){ int[] colorArray = {0,0,255}; } else if(shapeShade == "green"){ int[] colorArray = {0,255,0}; } } return colorArray; }

And it seems it should work, but it keeps telling me "expected EOF", how do I return this array? If it matters, I'm returning this to an array of the same size in the constructor of a class.

Tagged:

Answers

  • edited June 2015 Answer ✓

    http://forum.processing.org/two/discussion/8045/how-to-format-code-and-text

    • Variables are created when we declare their type like: int[] colorArray = {255, 0, 0};.
    • However, they exist inside a scope. And those colorArray are created within if scoped blocks.
    • So @ return colorArray;, variable colorArray doesn't exist anymore.
    • Another tricky bug is comparing String objects via == equality operator.
    • Please use equals() instead: https://processing.org/reference/String_equals_.html
  • Answer ✓

    Hmm... seems like you already had problems w/ scoping before: :-\"

    http://forum.processing.org/two/discussion/11221/class-not-working-what-am-i-doing-wrong

  • Answer ✓

    The function is also closed before the return statement: check your {braces}...

    I remember being bitten by the issue @GoToLoop refers to. Since colorArray gets declared only inside conditions there's a risk that it never gets declared. So declare the empty variable before the conditions, rather than repeatedly inside them... You could even remove a condition altogether: e.g. set its value to red and then override it when appropriate.

  • Answer ✓

    Since colorArray gets declared only inside conditions there's a risk that it never gets declared.

    I don't get what you meant by "risk". Declarations always overshadow other variables declared in outer scopes if they happen to have the same name until the end of the inner scope.

  • do all { } automatically make a new scope?

    like for and if () { } ?

  • @GoToLoop: my description wasn't meant to be overly technical, but the way I always thought about this situation was that I may know that one of the three conditions will always return true, so there should be a value to return, but the interpreter can't know this so reports it as an error...

    I guess on a technical level it simply can't find the variable declared in the same scope?

  • edited June 2015 Answer ✓

    ... my description wasn't meant to be overly technical,

    • Sorry about that! I was over worried that it could be misinterpreted by the OP. 8-|
    • Of course the very 1st bug is simply an extra too early closing } curly brace.
    • But that's easily spottable by hitting CTRL+T in the PDE... :-\"
    • Right after that is the "inexisting" colorArray variable when code reaches the return statement.
    • And finally the insidious bug of comparing String via == operator.
    • However, that is just a potential bug in case the String reference passed doesn't come from a literal "" 1, like "red", "blue" or "green". :P
  • edited June 2015

    ... like for () and if () { }?

    Be aware that any declaration inside for's parens is also its own separate scope,
    apart from its {}'s scope.
    For example, the following code won't compile due the "inexistence" of variable step,
    since it doesn't exist inside for () parens's scope: :-B

    for (int i = 0; i != 10; i += step) {
      int step = i*2 + 4;
    }
    
  • So if I declare the array like

    int[] colorArray = new int[3];

    then can I assign data later using

    colorArray = {255,0,0};

  • edited June 2015 Answer ✓

    The answer is: "Yes & No"! @-)

    • Variables can only store 1 value at a single time.
    • So when we use the = assign operator, the old stored value is discarded.
    • Therefore, it doesn't matter if we assign some array reference early on as in:
      int[] colorArray = new int[3];
    • Or simply leave it blank w/o some assignment as in: int[] colorArray;
    • Since it's gonna receive a brand new int[] array assignment down the road after all.
    • Now the reason I've also said "No"... ^#(^
    • The simplified array instantiation w/ just curly braces as in: { 255, 0, 0 }
    • is only valid when we're also declaring the variable at the same time as in:
      int[] colorArray = { 255, 0, 0 };
    • Otherwise for later reassignments, we are obliged to specify its type before the curly braces:
      colorArray = new int[] { 255, 0, 0 };

    P.S.: Processing got a pseudo type called color which is the same as int. ;;)
    https://processing.org/reference/color_datatype.html

Sign In or Register to comment.