Static vs. Dynamic Scoping

3 May, 2011 § 14 Comments

Have you ever heard of dynamic scoping? Most of us have worked in environments where static scoping is all we have, and probably for good reason.

One of the first things people think about when they hear the words “dynamic scoping” is dynamic memory, which is not at all what dynamic scoping is. Dynamic scoping could also be thought of as context-specific scoping.

C, C++, C#, and JavaScript all use static scoping, whereas Perl and Logo allow dynamic scoping though their local keyword.

Here’s an example to compare the two:

Static scoping Dynamic scoping
int b = 5;
int foo()
{
   int a = b + 5;
   return a;
}

int bar()
{
   int b = 2;
   return foo();
}

int main()
{
   foo(); // returns 10
   bar(); // returns 10
   return 0;
}
int b = 5;
int foo()
{
   int a = b + 5;
   return a;
}

int bar()
{
   int b = 2;
   return foo();
}

int main()
{
   foo(); // returns 10
   bar(); // returns 7
   return 0;
}

Dynamic scoping means that when a symbol is referenced, the compiler/interpreter will walk up the symbol-table stack to find the correct instance of the variable to use. This can be a cool tool to use when writing software, but also a huge source of errors if it is used accidentally.

Tagged:

§ 14 Responses to Static vs. Dynamic Scoping

  • A.J. says:

    Hey Hey Jared!

    Good example and explaining of how it would work. I think I might be able to have a use for dynamic scoping if it can help me with a dumb issue I keep running into.

    Say I have the following piece of code:
    string str = File.ReadAllText(strFilename);
    /*Then do something with str*/

    Now it may throw exceptions so I put it in a try/catch:
    try
    {
    string str = File.ReadAllText(strFilename);
    }
    catch(System.Exception ex)
    {
    }
    /*Then do something with str*/

    I then often find myself fixing the error of str is no longer in the same scope as where I am doing something with str. And so I was thinking maybe this local keyword or something might help me out in this situation. I hear you when you say it is Perl; maybe it will spread!

    • Svein Asleik says:

      I don’t think it will help with that. What you say you want to do is set str inside the try scope and then use it outside the try scope again. When you try to use it outside the try scope it will first be looked for in the current scope, then the outer scope etc. It won’t try to look for it in an inner scope. What dynamic scoping means is that it will “go backwards”/”outwards” and use the first declaration in the first scope it finds it in. If it goes all the way out to the outermost scope and still haven’t found it, it will give you an error saying it couldn’t find it.

      The solution to your problem is simple. Declare the variable in the outermost scope you want to use it in and set it in one of the inner. Example:
      String str
      try
      {
      str = File.ReadAllText(strFilename);
      }
      catch(System.Exception ex)
      {
      }
      /* You need to check if str is set or not (null) before you use it */

  • simple, clean and nice example.

  • merilyn says:

    thanks i was searching everywhere to find a correct example for this

  • Paul says:

    OMG THANK YOU MY HOMEWORK CAN’T FIND EXAMPLES NOWHERE

  • Ankit Arora says:

    static scope variables are stored on the heap during the compile so the value of the variable that lies in that procedure is considered.’
    Dynamic scope variables are stored on the stack during the run-time of the program and the most recent value on the stack is considered.

  • in dynamic scope what does[value] foo(), and bar() return?

    • msujaws says:

      As shown in the dynamic scope example, foo() returns 10 and bar() returns 7.

  • Piyush Wanjale says:

    In dynamic scope, when program finishes the execution, are the values of variables local to that function are removed from stack or not?

  • bradgnar says:

    javascript is dynamically scoped. this is wrong.

  • blolb says:

    Why do you declare the variable b as const – this would make it immutable, even though it is a global variable, and it would probably generate a compiler error because you are trying to mutate a variable that cannot be mutated. (in the dynamic scoping example)

    • msujaws says:

      Good question! It has been a while since I wrote this post, and I can’t see why I would have wanted that to be declared const. I’ll fix the post now.

Leave a comment

What’s this?

You are currently reading Static vs. Dynamic Scoping at JAWS.

meta