HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [156]
text += “ the little one stops to “;
text += distraction;
return text;
} // end verse1
A whole lotta’ concatenating is going on, but it’s essentially the same code as the original verse() function. This one’s just a lot more flexible because it can handle any verse. (Well, if the function has been preloaded to understand how to handle the verseNum.)
Managing Scope
A function is much like an independent mini-program. Any variable you create inside a function has meaning only inside that function. When the function is finished executing, its variables disappear! This setup is actually a really good thing. A major program will have hundreds of variables, and they can be difficult to keep track of. You can reuse a variable name without knowing it or have a value changed inadvertently. When you break your code into functions, each function has its own independent set of variables. You don’t have to worry about whether the variables will cause problems elsewhere.
Introducing local and global variables
You can also define variables at the main (script) level. These variables are global variables. A global variable is available at the main level and inside each function. A local variable (one defined inside a function) has meaning only inside the function. The concept of local versus global functions is sometimes referred to as scope.
Local variables are kind of like local police. Local police have a limited geographical jurisdiction, but they’re very useful within that space. They know the neighborhood. Sometimes, you encounter situations that cross local jurisdictions. This situation is the kind that requires a state trooper or the FBI. Local variables are local cops, and global variables are the FBI.
Generally, try to make as many of your variables local as possible. The only time you really need a global variable is when you want some information to be used in multiple functions.
Examining variable scope
To understand the implications of variable scope, take a look at scope.html:
This program defines two variables. In the main code, globalVar is defined, and localVar is defined inside a function. If you run the program in debug mode while watching the variables, you can see how they behave. Figure 4-2 shows what the program looks like early in the run.
localVar doesn’t have meaning until the function is called, so it remains undefined until the computer gets to that part of the code. Step ahead a few lines, and you see that localVar has a value, as shown in Figure 4-3.
Be sure to use Step Into rather than Step Over on the “remote control” toolbar for this example. When Step Over encounters a function, it runs the entire function as one line. If you want to look into the function and see what’s happening inside it (as you do here), use Step Into.
globalVar still has a value (it’s an FBI agent), and so does localVar because it’s inside the function.
Figure 4-2: globalVar is defined, but localVar is not.
If you move a few more steps, localVar no longer has a value when the function ends (see Figure 4-4).
Figure 4-3: localVar has a value because I’m inside the function.
Figure 4-4: Once again, localVar has no meaning.
Variable scope is a good thing because it means you have to keep track of only global variables and the variables defined inside your current function. The other advantage of scope is the ability to reuse a variable name. You can have ten different functions all using the same variable name, and they won’t interfere with each other because they’re entirely different variables.
Building a Basic Array
If functions are groups of code lines with a name, arrays are groups of variables with a name. Arrays are similar to functions because they’re used to manage complexity. An array is a special kind of variable. Use