Static vs. Dynamic Scoping

3 May, 2011 § 12 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
const 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;
}
const 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:

§ 12 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?

  • 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

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

meta

Follow

Get every new post delivered to your Inbox.

Join 1,003 other followers