PRINCIPLE OF PROGRAMMING LANGUAGES SCOPE STATIC AND DYNAMIC SNEH PAHILWANI SYMBIOSIS INSTITUTE OF TECHNOLOGY
In computer programming, a scope is the context within a computer program in which a variable name or other identifier is valid and can be used, or within which a declaration has effect. Outside of the scope of a variable name, the variable's value may still be stored, and may even be accessible in some way, but the name does not refer to it; that is, the name is not bound to the variable's storage.
SCOPE STATIC DYNAMIC
 Also called lexical scoping.  If a variable name's scope is a certain function, then its scope is the program text of the function definition: within that text, the variable name exists, and is bound to its variable, but outside that text, the variable name does not exist.
Output: 2 3 •Encounter occurrence of „x‟. •Search local block variables •If not there, keep searching in parent blocks. •If not there too, in global scope then. •Otherwise „error‟
SCOPE STATIC DYNAMIC
 In dynamic scoping (or dynamic scope), if a variable name's scope is a certain function, then its scope is the time-period during which the function is executing.  While the function is running, the variable name exists, and is bound to its variable, but after the function returns, the variable name does not exist.
Pointing the difference out •Under static scoping: prints out 2 2 •Under dynamic scoping: prints out 3 4 •At “runtime” one searches for the declaration •Search in local variables •If not found, search in local variables of the caller •If not found, search in global variables •Otherwise error
www.wikipedia.org http://cseweb.ucsd.edu/cl asses/sp05/cse130/lecture_ notes/scoping_student.txt
Scope - Static and Dynamic

Scope - Static and Dynamic

  • 1.
    PRINCIPLE OF PROGRAMMING LANGUAGES SCOPE STATIC AND DYNAMIC SNEH PAHILWANI SYMBIOSIS INSTITUTE OF TECHNOLOGY
  • 2.
    In computer programming,a scope is the context within a computer program in which a variable name or other identifier is valid and can be used, or within which a declaration has effect. Outside of the scope of a variable name, the variable's value may still be stored, and may even be accessible in some way, but the name does not refer to it; that is, the name is not bound to the variable's storage.
  • 3.
  • 4.
    Also called lexical scoping.  If a variable name's scope is a certain function, then its scope is the program text of the function definition: within that text, the variable name exists, and is bound to its variable, but outside that text, the variable name does not exist.
  • 5.
    Output: 2 3 •Encounter occurrence of „x‟. •Searchlocal block variables •If not there, keep searching in parent blocks. •If not there too, in global scope then. •Otherwise „error‟
  • 6.
  • 7.
    In dynamic scoping (or dynamic scope), if a variable name's scope is a certain function, then its scope is the time-period during which the function is executing.  While the function is running, the variable name exists, and is bound to its variable, but after the function returns, the variable name does not exist.
  • 8.
    Pointing the differenceout •Under static scoping: prints out 2 2 •Under dynamic scoping: prints out 3 4 •At “runtime” one searches for the declaration •Search in local variables •If not found, search in local variables of the caller •If not found, search in global variables •Otherwise error
  • 9.