Tuesday, December 4, 2012

Returning values from functions

To return a value from your function, use the return statement followed by the expression or literal value that you want to return. For example, the following code returns an expression representing the parameter 
function doubleNum(baseNum:int):int {
      return (baseNum * 2);
}
Note that the return statement terminates the function, so that any statements below a return statement will not be executed:
function doubleNum(baseNum:int):int {
     return (baseNum * 2);
     trace ("after return"); // this trace statement will not be executed
}
In strict mode, you must return a value of the appropriate type if you choose to specify a return type. For example, the following code generates an error in strict mode because it does not return a valid value:
function doubleNum(baseNum:int):int {
      trace ("after return");
}
Programming Actionscript 3. Powered by Blogger.