search
Log In
28 votes
4.4k 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 static scoping and call by need parameter passing mechanism, the values printed by the above program are:

  1. $115, 220$
  2. $25, 220$
  3. $25, 15$
  4. $115, 105$
in Programming
edited by
4.4k views
2
IS this in syllabus ?
2
I've also same question. Can anybody plz clarify different type of scope rule is in the syallabus or not?
1

@Arjun ji,

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

5 Answers

14 votes
 
Best answer

Answer : D

First refer the following question on Call-by-name parameter passing technique then solve this question.

https://gateoverflow.in/43575/gate2003-74?show=338119#a338119

Call by Name vs. Call by Need :

Assume X is the formal and e the corresponding actual expression. 

Call-by-Name : 

1. Delays evaluation of arguments past call until a reference to the formal.

2.  Re-evaluates argument e on each reference to X in environment of caller.

3.  No local variable X is allocated

Call-by-Need : 

1. Delays evaluation of arguments past call until a reference to the formal.

2.  Evaluates e on 1st reference in environment of caller & loads local variable X; no re-evaluation: subsequent references use local X


Since "Call by need"  parameter passing technique, it is almost same as Call-by-name But the difference is that Actual argument is evaluated only once(on the first reference) and then that value is saved and re-used on further references But the actual argument is Not re-evaluated.

Caller function's Actual argument contains variable $i$ which clashes with called function P's local variable $i,$ hence, we rename called function P's local variable $i$ and change it to $i'.$

global int i=100, j=5;
void P(x) {
	int i'=10;   // this i' refers to the local variable i' in function P.
    print(x+10);  // this is first reference of x, so here, x= i+j, and thses i,j refer to i,j in the caller function i.e. main function's environment
    i'=200;    // this i' refers to the local variable i' in function P.
    j=20;      // this j refers to j in the caller function i.e. main function's environment
    print (x);   // this x is second reference, so, we do not replace it with i+j because in call by need, we do not re-evaluate. So, we use the already calculated value of variable x and use it here.
}
main() {P(i+j);}

In case of Static scoping : 115, 105

In case of Dynamic scoping : 115, 105

Note that there are no local variable $i,j$ in main function, so, when we say that $i,j$ refer to the $i,j$ in main's environment , we mean that If $i,j$ were accessed/updated in main function then depending on the scoping, which $i,j$ would they refer.

Here, in this question, in both static and dynamic scoping case, $i,j$ will refer to the Global variables. 

And in function P, in the 4th statement (i.e. $j = 20$), the Global variable $j$ will be updated. 


selected by
32 votes

Call-by-name : Is a lazy evaluation (expression passed as an argument evaluated only when used) technique.

Call-by-need: Is a version of call-by-name but when an expression is evaluated during first use, is saved and reused for all later uses.

global int i=100, j=5; 
//memory created for i and j variable and 100 and 5 stored in them respectively (1)

void P(x) {// p(i+j) (3)
    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  
    // here need of i+j so i+j replced by 15 evrywhere .(5)
   
    i=200;
    // local i value changed to 200. 
    
    j=20;
    //global j value changed to 20 as there is no local j.
    //if dynamic scoping used, then the scope of j comes from scope of j in main as 
    //main called this function. Here, main also uses global j and hence no change.
    
    print (x);
    // print (x);= print (i+j); = printf(15) = 15 (7)
    // due to call by need. If call by name used answer is 200+20 = 220
}
main() {P(i+j);}  
// 1st function call since it is call by need no evaluation done 
//here i and j value refer to global values(2) 

Answer is (C).

Reference : https://www.cs.bgu.ac.il/~comp161/wiki.files/ps9.pdf


edited by
0
Is there no significance about static scoping??
2
Thanx for making correction.
0
its not in syllabus now right?
2
why should take a risk ??

it won't take more than hour to learn this concept .
0
okay sir :-(
1
@ Gabbar, Anirudh, Arjun sir

What would be the answer in case of pass by reference?? i+j will refer to what in call by reference??

Since expression first evaluates and then goes to function, will i+j make a new variable and its address will be passed?? please resolve this issue.
0
Very good explaination....Thnkx..
0
so, in case of static scoping and call by name, will print(x+10)... be 15 and print(x) is 220...

and in case of dynamic scoping and call by name will print(x+10)... be 115 or 25 .(But as far as i know answer should be same for static and dynamic scoping.The difference will come when you try to use a variable that is not available in scope.) and print(x) is 220...if so.. also how do i come to know that i=200 will change global i or local i.... Does that depend on static and dynamic scoping...Plzzz explain?

Also in case of both static scoping and dynamic scoping call by need,will print(x+10)... be 25 and print(x) be 15...?
0
It's a tricky quesion but it was nicely explained here.
0

call-by-name but when an expression is evaluated during first use, is saved and reused for all later uses.

for understanding see the wording carefully..

15 votes

In Gate official keys the answer for this question is given as the option (D) and for the next one it is (A) 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


 

  

1
I think answer should be same for static and dynamic scoping.The difference will come when you try to use a variable that is not available in scope.

In above dynamic scoping,you have used 100 instead of 10,which seems to be wrong as local variable are of high priority regardless of scope
0
not getting how call by name different from call by need in terms of    scoping
0
im really confused ,how in dynamic scoping you have not considered the local variable 🤔
2 votes
Static scoped and call by need..

Call by need means if any expression is evaluted that is stored and later on used when same expression is needed..

Here in first printf : I + j is evaluated..

Since it is of static scope..

I= 10 and j= 5 so I + j is 15

So first printf prints 25

Now second printf : I+j which is already evaluated that is 15

So it prints 15

Ans.C
0 votes
P(i+j)
P(100+5) = P(105)
→void P(105)
{   
int i=10;   
print (x+10);  ⇒ 105+10=115 prints
  i=200;  
  j = 20;  
  print (x);  ⇒ x=105 prints
}  
115, 105 prints
0
Your answer wrong if value of i available in local function why are you taking from global function
0
Call-by-need is a declarative and functional language paradigm. As opposed to call-by-value, it only evaluates the argument given to a function when it is need, and caches it for further use. Call-by-name is a bit different from call-by-need in the sense that it evaluates in the lazy fashion only but doesn’t cache the calculated value. Hence, call-by-need requires to evaluate once if the argument is being used, call-by-value needs to evaluate it no matter if the argument is used or not, and call-by-name evaluates the same argument multiple times as and when it is used.
0

Here in main function P(i+j) function is called with arguments values i =100 and j = 5(since static scoping).Now when P(100+5) is called, here in P function, j is a free variable and i is a local variable.Now take a look at the steps in P function :

step_1: Local variable i is intialized to value 0.

step_2: Here value of x+10 is printed and since it’s call by need,so value of x is calculated to 105 and it’s stored in cache for further use.Also 115 is printed here.

step_3: Now, i is assigned value 200.

step_4: Here, j is a free variable, So it’s scope is searched in immediate outer block until we find variable j’s declaration.Global variable j’s value is changed from 5 to 20.

step_5: x value is printed as 105.Since it’s uses the cache stored value of x.

The output would be similar to C programming language as C does static scoping only.

Answer:

Related questions

30 votes
6 answers
1
5.1k 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 ... scoping and call by name parameter passing mechanism, the values printed by the above program are $115, 220$ $25, 220$ $25, 15$ $115, 105$
asked Apr 24, 2016 in Compiler Design jothee 5.1k views
19 votes
3 answers
2
5.5k views
What is printed by the print statements in the program $P1$ assuming call by reference parameter passing? Program P1() { x = 10; y = 3; func1(y,x,x); print x; print y; } func1(x,y,z) { y = y + 4; z = x + y + z } $\text{10, 3}$ $\text{31, 3}$ $\text{27, 7}$ None of the above
asked Sep 15, 2014 in Programming Kathleen 5.5k views
54 votes
1 answer
3
9.9k views
What is the return value of $f(p,p)$, if the value of $p$ is initialized to $5$ before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value. int f (int &x, int c) { c = c - 1; if (c==0) return 1; x = x + 1; return f(x,c) * x; }
asked Aug 7, 2014 in Programming gatecse 9.9k views
42 votes
4 answers
4
8k views
Which of the following is NOT an advantage of using shared, dynamically linked libraries as opposed to using statistically linked libraries? Smaller sizes of executable files Lesser overall page fault rate in the system Faster program startup Existing programs need not be re-linked to take advantage of newer versions of libraries
asked Sep 17, 2014 in Compiler Design Kathleen 8k views
...