Thread: C/C++-: dynamically scoped language

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    48

    C/C++-: dynamically scoped language

    Hello, I was hoping that someone could help me with this question. We received an extra credit queston which completely bombed on. That wasnt the problem, the problem was that I still cannot explain the questions below and since this was extra credit the professor would not give us the answer. Is there a site or book that would explain this.

    Any help would be greatly appreciated

    Thanks in advance

    Code:
    void f ( )  {
        cout<<  "x is " << x << " and y is " << y << "\n";
    }
     
    void g ( )  {
        int y = 20;
        f ( ) ; 
    }
     
    void h ( ) {
        int y = 30;
        f ( );
    }
     
    void main ( ) {
        int x = 10;
        g ( );
    h ( );
    }

    1. Explain why the following program is illegal in standard C/C++.
    2. Referring to your answer to (1.), explain why the following program would be legal if C/C++ were made into a dynamically scoped language.
    3. If C/C++ were made into a dynamically scoped language, what would main () output?

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I sure hope part of the answer is that it uses void main() instead of int main()
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I just love answering a question on one site and then seeing the same question on another site.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    1. Explain why the following program is illegal in standard C/C++.
    Unless if there's some global variable declared above this fragment, the use of the "x" is before it's been defined or declared

    2. Referring to your answer to (1.), explain why the following program would be legal if C/C++ were made into a dynamically scoped language.
    By dynamically scoped, your professor means that the traversal of control in the program determines the scope of a variable. Another way is to think of an interpreter. When the statement "int x = 5" is reached, the variable x is inserted into the interpreter's store of variables. Before this point, x wasn't defined, but after it is. As soon as a variable is inserted into the interpreter, however, it stays in the interpreter. Any redefinition only changes the variable's value.


    3. If C/C++ were made into a dynamically scoped language, what would main () output?
    You should be able to answer this. When g is called in main, x is defined but y is undefined. g, then, defines y and calls f(), which prints out x and y. Control eventually returns to main and h() is called. y is redefined and f is called. When all these functions finish, the program finishes.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    48
    Thanks for all of your help everyone

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why C Matters
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 136
    Last Post: 01-16-2008, 09:09 AM
  2. What language did they make Java in?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 07-03-2005, 04:18 PM
  3. Strange loop
    By D@rk_force in forum C++ Programming
    Replies: 22
    Last Post: 12-18-2004, 02:40 PM
  4. Language of choice after C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 06-15-2004, 01:20 AM
  5. Languages dying
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 07-29-2003, 10:08 AM