CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2007
    Posts
    28

    Exclamation dynamic vs static scoping...

    hey everyone.. i am trying to understand the difference between dinamic and static scoping...

    can someone please explain me this short problem and what would be the results..
    Code:
          int a = 27;
    
              f() {
                int a = 32;
                g();
              }
    
              g() {
                 print a;
                 {
                   int a = 99;
                   h();
                 }
              }
    
              h() { print a; }
    
              print a;
              f();
              g();
              h();
    what i mean is what would be the output of the above program using static scope and the output using dynamic scop, assuming that each invocation of print a prints the value of a on a separate line.


    also.. is it true that dynamic scope is used in most popular programming languages??

  2. #2
    Join Date
    Dec 2006
    Posts
    166

    Re: dynamic vs static scoping...

    static scope would output
    27
    27
    27
    27
    27
    27
    dynamic scope would output
    27
    32
    99
    27
    99
    27

    basically all languages that are still used today use static scope, because dynamic scope is very difficult to reason about
    some ideas of dynamic scope are used in object-oriented programming with dynamic binding of virtual methods

  3. #3
    Join Date
    Aug 2007
    Posts
    28

    Thumbs up Re: dynamic vs static scoping...

    Quote Originally Posted by spoon!
    static scope would output
    27
    27
    27
    27
    27
    27
    dynamic scope would output
    27
    32
    99
    27
    99
    27

    basically all languages that are still used today use static scope, because dynamic scope is very difficult to reason about
    some ideas of dynamic scope are used in object-oriented programming with dynamic binding of virtual methods

    thank you soooo much

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: dynamic vs static scoping...

    Don't get that...

    F() doesn't PRINT anything
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Aug 2007
    Posts
    28

    Re: dynamic vs static scoping...

    Quote Originally Posted by dglienna
    Don't get that...

    F() doesn't PRINT anything
    it calls g function... did you find a mistake in other guy's answer?

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: dynamic vs static scoping...

    I don't see 6 Print statements
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Dec 2006
    Posts
    166

    Re: dynamic vs static scoping...

    1. there is a print before f is called
    2. g prints
    3. h prints
    4. g prints
    5. h prints
    6. h prints

  8. #8
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: dynamic vs static scoping...

    Oh. I see now. Thanks.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured