The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+19 votes
1.7k views

The following program fragment is written in a programming language that allows global variables and does not allow nested declarations of functions.

global int i=100, j=5;
void P(x) {
	int i=10;
    print(x+10);
    i=200;
    j=20;
    print (x);
}
main() {P(i+j);}

If the programming language uses dynamic scoping and call by name parameter passing mechanism, the values printed by the above program are

  1. $115, 220$
  2. $25, 220$
  3. $25, 15$
  4. $115, 105$
asked in Compiler Design by Veteran (105k points)
edited by | 1.7k views
0

@Arjun ji,

I think whatever @nvedansh ji is saying is correct. Other  reader please check and Provide your valuable suggestion.

4 Answers

+25 votes
Best answer
global int i=100, j=5; // memory created 
//for i and j variable and 100 and 5 store in them respectively (1)

void P(x) { // p(i+j) (3)
//dynamic scoping means when a variable is not found locally, 
//it refers to the caller function for it 

    int i=10; // new variable created i with value 10 store in it. (4)

    print(x+10); // print(x+10); = print(i+j +10);= 10 +5 +10 = 25 (5)

    i=200; // local i and global j value changed to 200 and 20 respectively  (6)

    j=20;
    print (x); // print (x);= print (i +j); = 200 +20 = 220 (7)
}
main() {P(i+j);}  // 1st function call 
//since it is call by name dont calculate value just send i+j  
//Since there are no local i and j, they refer to the global variables (2) 

Answer is (B).

Refer: http://stackoverflow.com/questions/838079/what-is-pass-by-name-and-how-does-it-work-exactly

answered by Veteran (61.1k points)
edited by
+1

@Anirudh in ur solution "global i and j value changed to 200 and 20 respectively  (6) "

but if u doing this that means at the end u should get 30 since in first printf u have given priority to local bariable i...

0
Why x not equal to 105?
+2
Thumps up  for explanation
0
In which type of parameter passing it will send x=105 to p ???
+4 votes

In Gate official keys the answer for this question is given as the option (A) and for the previous one it is (D) and that is the correct solution.

Call-by-Name: Here the expression is evaluated every time it is requested, no cache is stored.  

Call-by-Need: Here the expression is evaluated only once and it is stored for further reference, no cache is stored.  

In static scoping, the local variables have more preference than global variables if the name of both the variables is same. In dynamic scoping, the global variables are preferred over local variables. 

Now let us look at the program.

Static scoping and Call-by-need

global int i=100, j=5;
void P(x) {
	int i=10; // Local variable declared and initialized to value 10
    print(x+10); //Requested the evaluation of the expression, but the expression uses global i not local i so x + 10 = i + j +10 = 105 +10 =115 
    i=200; // Local i assigned to 200 as static scoping prefers local variable
    j=20; // No confusion here, the global value is changed to 20
    print (x); // Here again as it is Call-by-Need and is only evaluated once so this will print 105 as x is 105
}
main() {P(i+j);} // Here the expression i+j is passed but it is not evaluated yet.

Here in static scoping the local variable is preferred over global but that does not mean that in print(x) line, i is local variable but it is the global one as the expression is evaluated from the scope of the main() function from which it was called.
So the answer is (D)


Dynamic scoping and Call-by-name

global int i=100, j=5;
void P(x) {
	int i=10; // Local variable declared and initialized to value 10
    print(x+10); //Requested the evaluation of the expression, but the expression uses global i not local i so x + 10 = i + j +10 = 105 +10 =115 
    i=200; // Here is the difference where assignment of is done on the global one not the local one
    j=20;// Here global value is changed.
    print (x); // Now again expression is requested as it is call by name and now as the global values have changed so the answer is 200 + 20 = 220 
}
main() {P(i+j);} // Here the expression i+j is passed but it is not evaluated yet.

(A) is the answer

answered by (313 points)
+2
FYI: There were no official keys before GATE 2011. And the explanation given is wrong --  The terms global and local alone are not enough while explaining static/dynamic scoping.
+3 votes

Answer will be B dynamic scope and call by name is used so first print will be evaluated as

            print(10+5+10)

and 2nd print will be evaluated as

           print(200+20)

answered by
0
Can you please explain the same with static scoping and call by name
+2 votes

ans is A

answered by Active (2.6k points)
Answer:

Related questions



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

42,557 questions
48,550 answers
155,299 comments
63,512 users