Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 3. Working with Functions and Me... > Returning Values from a Function

Returning Values from a Function

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.

Code 3.5. The value of the variable numberSum is set by the return value from addNumbers().

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);


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial