Friday, May 6, 2011

AS3 odd or even (mal)function

please how to fix this AS3 function ?

thank you

function dispari(numero:int):Boolean;
{
  //check if the number is odd or even
  if (numero % 2 == 0)
  {
    returns false;
  }
  else
  {
    returns true;
  }
}

ERROR: 1071: Syntax error: expected a definition keyword (such as function) after attribute returns, not false.

From stackoverflow
  • Why do you have a semi-colon (;) at the end of your function statement? I don't do any AS3 coding but it doesn't look right, and a cursory glance at a few samples on the web don't have it there.

    I suspect that may be what's causing your problem. Try this instead:

    function dispari(numero:int):Boolean
    {
        //check if the number is odd or even
        if (numero % 2 == 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    

    I've also changed the return statements to match what every other piece of AS3 does to return values (thanks, @Herms, forgot to mention that :-)

    Herms : exactly. The ; shouldn't be there.
    Herms : Oh, also note that it should be "return" not "returns". This answer's code has it correct.
  • Pax is correct with there answer, but I would also use the conditional operators for this small test.

    function dispari(numero:int):Boolean
    {
      return (numero % 2 != 0);
    }
    
    paxdiablo : or "return (numero % 2 != 0);" ?

0 comments:

Post a Comment