VizmayaTech





Static scoping and Dynamic scoping

October 17th, 2005 by eDeveloper in Programming

A scope is a defined context inside which certain defined entities of a programming language are visible – and outside which it is NOT visible. Various programming languages have different scope rules – of which particular interest is static and dynamic scoping.

In static scoping, the scope of the identifier is determined at compile time itself. A static scoped language resolves the identifier to a declaration at compile time using the scope rules. An example of statically scoped language is C, where every identifier is resolved to either a fixed address or to an offset inside the activation record during compilation. Since every identifier is uniquely bound to an address and scoping is determined, there is no runtime overhead in computing the scope of identifiers.

In dynamic scoping, the scope of the identifier is known only at runtime. In this case, the identifiers that are to be accessed by a procedure will be known only after it is invoked. Depending from where it was invoked, the procedure may have access to different identifiers. This means that the scope of the identifier is determined by the call sequence at runtime, which the compiler cannot determine. Since the scoping needs to be computed during runtime, runtime overhead exists to resolve the value of an identifier when encountered during execution.

Static scope is considered better because the scoping will be clear in the program code itself. This results in very readable programs. Dynamic scoped programs are difficult to read and maintain, since determining the scope and values of identifiers by analyzing the program code will be confusing for the programmer.

5 Responses to ' Static scoping and Dynamic scoping '

Subscribe to comments with RSS or TrackBack to ' Static scoping and Dynamic scoping '.

  1. jordy said,
    on March 25th, 2009 at 9:04 am

    good explanation

  2. Nad said,
    on February 13th, 2010 at 1:45 am

    :lol: :lol::lol::lol::lol::lol::lol:

  3. Ras said,
    on February 22nd, 2010 at 12:31 pm

    very well explained. thanks.:cool:

  4. vineet said,
    on April 20th, 2010 at 10:23 am

    not upto the mark

  5. divya said,
    on June 9th, 2010 at 12:48 am

    the explaination is nice bt some ex shud also b included!!!

Leave a reply

:mrgreen: :neutral: :twisted: :shock: :smile: :???: :cool: :evil: :grin: :oops: :razz: :roll: :wink: :cry: :eek: :lol: :mad: :sad: