Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Functions can give back as well as accept values. One of the real powers of functions is to reuse code that performs a common bit of functionality. For example, the previous walk-through that added the two number parameters together could return that value back to the caller as a value. This is similar to calling a variable to get its value. The code to return the value of sum is shown in Code 3.5.
var number1:Number = 5; var number2:Number = 2; var numberSum:Number; function addNumbers(pNum1:Number, pNum2:Number) { var sum:Number = pNum1 + pNum2; return sum; } numberSum = addNumbers(number1, number2); trace('The sum of number1 and number2 is: ' + numberSum); |