1

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:

  1. 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.
  2. 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.
  3. For static/ pass-by-value, my answer is 6, 4

  4. for dynamic/ pass-by-value, my answer is 7, 3
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Davidw
  • 31
  • 4
  • 1
    Possible duplicate of [Static (Lexical) Scoping vs Dynamic Scoping (Pseudocode)](https://stackoverflow.com/questions/22394089/static-lexical-scoping-vs-dynamic-scoping-pseudocode) – DBS Jan 09 '18 at 14:25
  • Though it's a Q&A site, you should not just re-post your homework here and expect it's be done. If you have a specific problem, describe it and we will try to assist. Tell us how far you got on your own and where exactly you are stuck. – PM 77-1 Jan 09 '18 at 14:25
  • @PM77-1 it's not a homework. I just come across to this question. I had my solution as follow: For static and pass-by-reference 9, 4 for dynamic and pass-by-reference is 13, 3 for static and pass-by-value 6,4 for static and pass-by-value 7, 3 – Davidw Jan 09 '18 at 14:40
  • Please add your own answers and your reasoning behind them to your question (use [edit]). – PM 77-1 Jan 09 '18 at 14:42
  • @PM77-1 please see my comment, i'm not hundred sure if my analysis is correct. – Davidw Jan 09 '18 at 15:03
  • Have you looked at the linked post yet? – PM 77-1 Jan 09 '18 at 15:09
  • I read. I'm clear about static scope and dynamic scope. But when it's mixed with pass-by-reference. I start getting confused. – Davidw Jan 09 '18 at 15:23

0 Answers0