Consider the following Pseudo-code: 1. procedure main 2. a : integer := 1 3. b :
ID: 3622042 • Letter: C
Question
Consider the following Pseudo-code:1. procedure main
2. a : integer := 1
3. b : integer := 2
4. procedure middle
5. b : integer := a
6. procedure inner
7. print a, b
8. a : integer := 3
9 end
10. -- body of middle
11. inner()
12. print a, b
13. end
14. -- body of main
15. middle()
16. print a, b
17. end
Suppose this was code for a language with the declaration-order rules of C ( but with nested subroutines) -- that is, names must be declared before use, and the scope of a name extends from its declaration through the end of the block. At each print statement, indicate which declarations of a and b are in the referencing enviroment. What does the program print (or will the compiler identify static semantic errors)? Repeat the exercise for the declaration-order rules of C# (names must be declared before use, but the scope of a name is the entire block in which it is declared) and of Modula-3 (names can be declared in any order, and their scope is the entire block in which they are declared).
Explanation / Answer
Dear... From the given Pseudo code, we can menction declaration order rues for C, C# and Modula-3 as followes. For C: Step1: integer a declared in main as 1. Step2: integer b declared in main as 2. Step3: function middle is executed. Step4: a new local variable b is set to global variable a as 1. Step5: a new local variable a is set to 3. Step6: function inner is executed and returns a as 3 and b as 1. Step7: prints a as 3 and b as 1. Step8: last, main prints a as 1 and b as 2. For C#: Step1: integer a declared in main as 1. Step2: integer b declared in main as 2. Step3: function middle is executed. Step4: global variable b is set to global variable a as 1. (Possible SEMANTIC ERROR? not sure..) Step5: global variable a is set to 3. (Possible SEMANTIC ERROR? not sure..) Step6: function inner is executed and returns a as 3 and b as 1. Step7: prints a as 3 and b as 1. Step8: last, main prints a as 3 and b as 1. For Modula-3: Step1: integer a declared in main as 1. Step2: integer b declared in main as 2. Step3: function middle is executed. Step4: a new local variable b is set to global variable a as 1. Step5: a new local variable a is set to 3. Step6: function inner is executed and returns a as 1 and b as 2. Step7: prints a as 1 and b as 2. Step8: last, main prints a as 1 and b as 2.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.