Question

In: Computer Science

(4 pts) What does the function fun print if the language uses static scoping? What does...

(4 pts) What does the function fun print if the language uses static scoping? What does it print with dynamic scoping?

x: integer <-- global procedure

Assignx(n:integer)

x:= n

proc printx

write_integer(x)

proc foo

Assignx(1)

printx

procedure boo

x: integer

Assignx(2)

printx

Assignx(0)

foo()

printx boo()

printx

Solutions

Expert Solution

Answer;

For static scoping:

It will print:

1

1

2

2

Explanation:

First Assignx sets global x as 0.

Outputs:

Line 1: foo() is called which assign 1 to the global x and then prints it i.e. 1

Line2: printx is called which prints the global x which was set to 2 in the previous step

Line3: boo() is called which assigns 2 to the global x by calling Assignx and then prints it i.e. 2

Line 4: printx is called which prints the global x which was set to 2 in the previous step

For dynamic scoping: (function call stack is checked for latest scope)

It will print:

1

1

2

1

First Assignx(0) sets global x since no function is called yet

Outputs:

Line 1: foo() is called and assigns 1 to global x and prints it as no other local x are declared in it.

Line 2: printx is called with global x

Line 3: boo() is called and it has local x declared, so latest scope of x is that inside boo()'s. So boo() calls assignx and sets its local x to 2 and then calls printx with this same x for printing it.

Line 4: finally printx is called with global x's scope which prints 1 as it was set in line 2

Thanking you...


Related Solutions

2. Let the function fun be defined as int fun(int*k) {       *k += 4;       return...
2. Let the function fun be defined as int fun(int*k) {       *k += 4;       return 3 * (*k) - 1; } Suppose fun is used in a program as follows: void main() {       int i = 10, j = 10, sum1, sum2;       sum1 = (i / 2) + fun(&i);       sum2 = fun(&j) + (j / 2); } What are the values of sum1 and sum2 a. operands in the expressions are evaluated left to right? b. operands...
Assume the following JavaScript program was interpreted using static-scoping rules. What value of x is displayed...
Assume the following JavaScript program was interpreted using static-scoping rules. What value of x is displayed in function sub1? Under dynamic-scoping rules, what value of x is displayed in function sub1? var x; function sub1() { document.write("x = " + x + ""); } function sub2() { var x; x = 10; sub1(); } x = 5; sub2();
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The...
Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The function will have one int 1D array n and one int size as parameters. The function will display all the values of n on one line. Function #2 Write the function AverageEvenOdd. The function will have one int 1D array n and one int size as parameters. The size of the array is given by the parameter int size. Therefore, the function should work...
What does the static keyword mean?
What does the static keyword mean?
1. Consider the program below. a. What would the output be in a language with static...
1. Consider the program below. a. What would the output be in a language with static scoping rules? Explain your answer. b. What would the output be in a language with dynamic scoping rules? Explain your answer. int x; //global int main() {    x = 11;    fun1();    fun2(); } void fun1() {    int x = 19;    fun3(); } void fun2() {    int x = 4;    fun3(); } void fun3() {    print(x); }
Write a program named subtract.asm that does the following using LC3 assembly language: Print a prompt...
Write a program named subtract.asm that does the following using LC3 assembly language: Print a prompt "PRESS TWO KEYS: " Note that the prompt MUST LOOK EXACTLY like the above, with a colon and a space after the word KEYS with no newline. Get a two key press from the user. Subtract the second characters ASCII code from the first characters ASCII code. If the result is positive, print the word POSITIVE. Turn in subtract.asm to the appropriate submission point...
4. [22 pts] For the function ?(?) = 2?5 − 9?4 + 12?3 − 12?2 +...
4. [22 pts] For the function ?(?) = 2?5 − 9?4 + 12?3 − 12?2 + 10? − 3 answer the following: a. [2 pts] Determine whether the function represents a polynomial. Justify your answer. b. [4 pts] Determine whether the function satisfies the Intermediate Value Theorem on the interval [0, 5]. Justify your answer. c. [2 pts] Determine the number (quantity) of complex zeros that the function has, provided the each zero is counted by its multiplicity. d. [4...
2. A manufacturer of chocolate candies uses machines to package candies as they (4 pts) move...
2. A manufacturer of chocolate candies uses machines to package candies as they (4 pts) move along a filling line. The company wants the packages to contain 8.1730 ounces of candy. A sample of 50 packages is randomly selected periodically and the packaging process is stopped if there is evidence to show that the mean amount is different from 8.1730 ounces tested at a 95.00% confidence interval/level. In one particular sample of 50 packages, the MEAN.S is equal to 8.1590...
What is language and what role does it play in thinking?
What is language and what role does it play in thinking?
What role does the cerebellum play in language?
What role does the cerebellum play in language?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT