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

Chapter 12. Functions > Nested Functions

Nested Functions

Nested functions can be a great tool if you want to run a repetitive set of scripts within a function but use the result differently in each function. Let's take a look at a couple of functions—the first will square a user-defined number, and the second will combine two squared numbers by using the return value from the first function:

function square(num:Number):Number{
    return num*num;
}
//Now create the second function
function combineSquares():Number{
    var square1:Number = square(2);
    var square2:Number = square(3);
    return square1 + square2;
}
//Set the variable to the second function
myNum = combineSquares();
trace(myNum);
//output: 13


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint