11.07.2015 Views

Encyclopedia of Computer Science and Technology

Encyclopedia of Computer Science and Technology

Encyclopedia of Computer Science and Technology

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

VBScript 491The value <strong>of</strong> the memory location labeled “Total” isobtained.The two values are added together.The result is stored in the location labeled “Total,” replacingits former value.Scope <strong>of</strong> VariablesIn early programming languages variables were generallyglobal, meaning that they could be accessed <strong>and</strong> changedfrom any part <strong>of</strong> the program. While this practice is convenient,it became riskier as programs became larger <strong>and</strong> morecomplex. One part <strong>of</strong> a program might be using a variablecalled Total or Subtotal to keep track <strong>of</strong> some quantity. Later,another part is written to deal with some other calculation,<strong>and</strong> uses the same names. The programmer may think <strong>of</strong> thesecond Total <strong>and</strong> Subtotal as being quite separate from thefirst, but in reality they refer to the same memory locations<strong>and</strong> any change affects both <strong>of</strong> them. Thus, it’s easy to createunwanted “side effects” when using global variables.Starting in the 1960s <strong>and</strong> more systematically duringthe 1970s, there was great interest in designing computerlanguages that could better manage the structure <strong>and</strong> complexity<strong>of</strong> large programs (see structured programming).One way to do this is to break programs up into more manageablemodules that each deal with some specific task (seeprocedures <strong>and</strong> functions). Unless explicitly declared tobe global, variables within a procedure or function are localto that unit <strong>of</strong> code. This means that if two procedures bothhave a variable called Total, changes to one Total do notaffect the other.Generally, in block-structured languages such as Pascala variable is by default local to the block <strong>of</strong> code in whichit is defined. This means it can be accessed only within thatblock. (Its visibility is said to be limited to that block.) Thevariable will also be accessible to any block that is nestedwithin the defining block, unless another variable with thesame name is declared in the inner block. In that case theinner variable supersedes the outer one, which will not bevisible in the inner block.Some languages such as APL <strong>and</strong> early versions <strong>of</strong> LISPdefine scope differently. Since these languages are not blockstructured, scope is determined not by the relationship <strong>of</strong>blocks <strong>of</strong> code but by the sequence in which functions arecalled. At run time each variable’s definition is searchedfor first in the code where it is first invoked, then in whateverfunction called that code, then in the function thatcalled that function, <strong>and</strong> so on. As with dynamic binding,dynamic scooping <strong>of</strong>fers flexibility but at a considerableprice. In this case, the price is that the program’s effects onvariables will be hard to underst<strong>and</strong>, <strong>and</strong> the search mechanismslows down program execution. Dynamic scoping isthus not <strong>of</strong>ten used today, even in LISP.Global variables were convenient because they allowedinformation generated by one part <strong>of</strong> a program to beaccessed by any other. However, such accessibility can beprovided in a safer, more controlled form by explicitly passingvariables or their values to a procedure or functionwhen it is called (see procedures <strong>and</strong> functions).Object-oriented languages provide another way to controlor encapsulate information. Variables describing dataused within a class are generally declared to be private(accessible only within the functions used by the class).Public (i.e., global) variables are used sparingly. The ideais that if another part <strong>of</strong> the program wants data belongingto a class, it will call a member function <strong>of</strong> the class, whichwill provide the data without giving unnecessary access tothe class’s internal variables.A final concept that is important for underst<strong>and</strong>ing variablesis that <strong>of</strong> lifetime, that is, how long the definition <strong>of</strong> avariable remains valid. For efficiency, the runtime environmentmust deallocate memory for variables when they canno longer be used by the program (that is go “out <strong>of</strong> scope”).Generally, a variable exists (<strong>and</strong> can be accessed) only whilethe block <strong>of</strong> code in which it was defined is being executed(including any procedures or functions called from thatblock). In the case <strong>of</strong> a variable declared in the main program,this will be until the program as a whole reaches itsend statement. For variables within procedures or functions,however, the lifetime lasts only until the procedure or functionends <strong>and</strong> control is returned to the calling statement.However, languages such as C allow the special keywordstatic to be used for a variable that is to remain in existenceas long as the program is running. This can be useful whena procedure needs to “remember” some information betweenone call <strong>and</strong> the next, such as an accumulating total.Further ReadingKernighan, Brian W., <strong>and</strong> Dennis Ritchie. The C Programming Language.2nd ed. Englewood Cliffs, N.J.: Prentice Hall, 1988.Sebesta, Robert W. Concepts <strong>of</strong> Programming Languages. 8th ed.Boston: Addison-Wesley, 2007.Stroustrup, Bjarne. The C++ Programming Language. special ed.Reading, Mass.: Addison-Wesley, 2000.VBScriptDating back to the mid-1990s, VBScript is a scripting languagedeveloped by Micros<strong>of</strong>t <strong>and</strong> based on its popularVisual Basic programming language (see basic <strong>and</strong> scriptinglanguage). It is also part <strong>of</strong> the evolution <strong>of</strong> whatMicros<strong>of</strong>t called “active scripting,” based on componentsthat allow outside access to the capabilities <strong>of</strong> applications.The host environment in which scripts run is providedthrough Windows (as with Windows Script Host) or withinMicros<strong>of</strong>t’s Internet Explorer browser.For client-side processing, VBScript can be used to writescripts embedded in HTML pages, which interact with thest<strong>and</strong>ard Document Object Model (see dom) in a way similarto other Web scripting languages (in particular, seeJavaScript). However, VBScript is not supported by popularnon-Micros<strong>of</strong>t browsers such as Firefox <strong>and</strong> Opera, sodevelopers generally must use the widely compatible Java-Script instead. VBScript can also be used for processing onthe Web server, particularly in connection with Micros<strong>of</strong>t’sWeb servers (see active server pages).Because versions <strong>of</strong> Windows starting with Windows98 include Windows Script Host, VBScripts can also bewritten to run directly under Windows. One unfortunate

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!