I'm learning difference between static scope and dynamic scope language. Here is a snippet pseudo code. write(x) will print out the value of x.
{
int x = 2;
void fie(reference int y) {
y = x + y;
x = x + 1;
}
{
x = 3;
{
int y = 5;
int x =6;
fie(x);
write(x);
}
}
write(x);
}
What's the output for static/pass-by-reference, dynamic/pass-by-reference, static/passed-by-value, dynamic/pass-by-value?
Here are my thoughta:
For static/ pass-by-reference, answer is 9 and 4. For the first output: fie(x) is passed with 6. in fie function, first line y = x + y, here, x is 3 (because it's static, it reads x value from global scope, which is 3), now y becomes y = 3 + 6, y becomes 9. As it's passed by reference, the value of y is changed, x is also changed, so the first write(x) output 9.
- For the second output: x is read from global scope. x is first assigned with 3 and added by 1 in the fie function, so output is 4.
for dynamic/pass-by-reference, my answer is 13, 3. I'm not quest sure about it. For the first output: fie(x) is passed with 6. In fie function, first line: y = x + y. y is 6. x is read from the calling scope, so the value of x is 6. now y = 6 + 6. y becomes 12. Because it's passed-by-reference, x also becomes 12, x = x + 1, x becomes 13. That's why the first out I think is 13.
- For the second output: x is read from global scope, so the output is 3.
For static/ pass-by-value, my answer is 6, 4
- for dynamic/ pass-by-value, my answer is 7, 3