This is a simple program to test static and dynamic scope. However, I can't output the answer using a dynamic scope because C++ uses a static scope.
Definitions:
Static scope - The scope of a variable is based on spatial (block structure) relationship of subprograms (functions)
Dynamic scope - The scope of a variable is based on the calling sequence of subprograms (functions), not on the block structure.
#include <iostream>
using namespace std;
int a;
int b;
int p()
{
int a, p;
a = 0;
b = 1;
p = 2;
return p;
}
void print()
{
cout << a << " " << b << endl;
}
void q()
{
int b;
a = 3;
b = 4;
print();
}
int main()
{
a = p();
q();
}
This code outputs:
3
1
using a static scope, but what would it output with a dynamic scope instead?
My guess is
3
4
but I'm not sure as I have no way to test C++ code using a dynamic scope
Second question: Is there an accessible way to test code examples like this using dynamic scope instead of static for learning purposes?