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?

Dani AI

Generated

Short answer: the program is illegal in standard C/C++ because f refers to names that are not in its lexical (compile-time) scope. C and C++ use static/lexical scoping: name lookup is determined by the source-code nesting, not by who called the function at runtime. A local variable declared inside main (or inside g/h) is not visible inside an unrelated top-level function f, so the compiler will reject uses of x and y inside f.

What “dynamic scoping” means here: identifier lookup is done by searching the run-time call chain for the most recent active binding. In that model, when f is called from g the stack has main -> g -> f, so x resolves to main’s 10 and y resolves to g’s 20. When f is later called from h, y resolves to 30. Thus the output under dynamic scoping would be exactly:
x is 10 and y is 20
x is 10 and y is 30
(’s macro expansion demonstration effectively mimics this by textually placing f’s body into the caller.)

Practical, portable ways to get the same behavior in real C++: pass the needed names as parameters (for example void f(int x, int y)), or capture them in a callable created inside main (a lambda that closes over x and is then passed into g/h). Globals or compiler-specific nested-function extensions can also produce similar results but have downsides.

Note: dynamic scoping can make reasoning and refactoring harder because bindings depend on the call stack rather than the program text; that is why lexical scoping is the norm in modern systems programming languages.

Recommended Answers

All 2 Replies

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