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

Chapter 4. Working with Functions > Understanding Local Versus Global Variables

Understanding Local Versus Global Variables

In Listing 4.5, notice how there are three variables declared outside (that is, before) the function and one variable declared inside the function. Why didn't I simply declare all the variables together at the top of the script? Because there's a big difference between variables that are declared outside of functions and those that are declared inside of functions. This section explains this crucial difference.

In programming, the scope of a variable defines where in the script a variable can be used and where it can't be used. To put it another way, a variable's scope determines which statements and functions can access and work with the variable. There are two main reasons you need to be concerned with scope:


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


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