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

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?

>1. Explain why the following program is illegal in standard C/C++.
It's illegal for more reasons than your instructor probably knows. ;)

First, cout is used without including <iostream> and qualifying for the std namespace. Second, main doesn't return void, it returns int. Third, variables can only be used in the scope that they're declared. x is declared in main, therefore it can't be used in f unless passed as an argument. The same thing goes for y.

The code assumes that a function call is expanded inline as if it were a macro. This works (it's not very bright though):

#include <iostream>

using namespace std;

#define f() {\
  cout<<  "x is " << x << " and y is " << y << "\n";\
}\

#define g() {\
  int y = 20;\
  f ( ) ; \
}\

#define h() {\
  int y = 30;\
  f ( );\
}\

int main ( ) {
  int x = 10;
  g ( );
  h ( );
}

>2. explain why the following program would be legal if C/C++ were made into a dynamically scoped language.
A dynamically scoped language basically does what my little inline macro expansion does. Names can be used in the scope that they're declared, and in the scope of nested function calls as well.

>3. If C/C++ were made into a dynamically scoped language, what would main () output?
Run my program, it's valid C++ code and will give you the same results as this hypothetical dynamically scoped C++.

tHANKS A LOT nARUE

I will check the code you included tonight

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.