2.2k 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$
edited | 2.2k views
0
IS this in syllabus ?
0
I've also same question. Can anybody plz clarify different type of scope rule is in the syallabus or not?
0

@Arjun ji,

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

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) 

answered by Veteran (61.4k points)
edited
0
Is there no significance about static scoping??
+2
Thanx for making correction.
0
its not in syllabus now right?
+1
why should take a risk ??

it won't take more than hour to learn this concept .
0
okay sir :-(
0
@ 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..

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

answered by (313 points)
0
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
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
answered by Boss (25.3k points)
Answer will be D since, its static scope is used x value will be fixed during compile time itself.

1
2
3
4
5
6