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

Chapter 15. Combining Statements into Fu... > Using var to Create Local Function V...

Using var to Create Local Function Variables

Most functions contain one or more variables used only within the function. For instance, suppose you’re writing a function, greet(), that returns one of three greetings (“good morning,” “good afternoon,” or “good evening”) depending on the time of day. To determine the time in ActionScript, you create both a Date object (such as myDate) and a variable to hold the time (such as hours). You want to use myDate and hours only within the greet() function, but you may want to use those names again elsewhere in the program to refer to different objects or variables. Looking to the future, you also want to be able to drop this function into other programs and know that it will not cause a “name conflict” by using a name that is already in use in that program and therefore affect the items (such as variables or objects) in the other program with which it is now inadvertently sharing the name.

In a situation like this, you can use the var keyword to declare a local variable within a function. A local variable is available only within the function. A local variable does not change or refer to a variable of the same name outside the function. For instance:


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


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