wood Posted November 1, 2003 Share Posted November 1, 2003 Consider the following procedure declaration: procedure p; x: integer; procedure q; begin x := x+1 end; procedure r; x: integer; begin x := 1; q; write(x) end; begin x:= 2; r end; What is the output produced by calling p in 1) A language with static scope 2) A language with dynamic scope Quote Link to comment Share on other sites More sharing options...
AlbaLed Posted November 1, 2003 Share Posted November 1, 2003 The topic I am currently working on hahaa:D Static scope. 1 Dynamic scope. 2 Quote Link to comment Share on other sites More sharing options...
wood Posted November 1, 2003 Author Share Posted November 1, 2003 A+ ! Wood Quote Link to comment Share on other sites More sharing options...
nonevent99 Posted November 2, 2003 Share Posted November 2, 2003 static: 1 dynamic: 2 Here's another one for you. Print the output in static and dynamic scoping assuming pass by value. Then repeat using pass by reference. int x = 1; procedure P(i) { int x = 1; i++; Q(i); } procedure Q(j) { j = j + x; } P(x) print x P(x) print x Quote Link to comment Share on other sites More sharing options...
AlbaLed Posted November 2, 2003 Share Posted November 2, 2003 int x = 1; procedure P(i) { int x = 1; i++; Q(i); } procedure Q(j) { j = j + x; } P(x) print x P(x) print x pass by value, Static scope prints 1 1 pass by value, Dynamic scope prints 1 1 pass by reference, Static scope prints 4 10 pass by reference, Dynamic scope prints 3 5 Correct??? Quote Link to comment Share on other sites More sharing options...
nonevent99 Posted November 2, 2003 Share Posted November 2, 2003 Alba, you must be the scope and variable king. Your answers match mine, though I did have to re-do static+byref twice before I was able to verify your answer. Good job! Quote Link to comment Share on other sites More sharing options...
AlbaLed Posted November 2, 2003 Share Posted November 2, 2003 Try solving some problems in this array, it is not hard, and you'll end up king too, we'll compromise for the throne:D Quote Link to comment Share on other sites More sharing options...
wood Posted November 2, 2003 Author Share Posted November 2, 2003 Great one guys! Quote Link to comment Share on other sites More sharing options...
munnymun Posted November 6, 2003 Share Posted November 6, 2003 hi guys, i'm new here. umm.. I don't understand this question. could you please tell me why X is not just 1. coz when I look at the code, it doesn't change the value of X, so why print X is not just 1. Thanks Quote Link to comment Share on other sites More sharing options...
nonevent99 Posted November 6, 2003 Share Posted November 6, 2003 Originally posted by munnymun hi guys, i'm new here. umm.. I don't understand this question. could you please tell me why X is not just 1. coz when I look at the code, it doesn't change the value of X, so why print X is not just 1. Thanks Wood was asking us to try out his code using dynamic scoping and static scoping. These are explained in depth in Aho's "Dragon Book" concerning compilers. The basic gist is that when you statically scope a variable, its binding is inherited from the enclosing scope. This what Pascal does, for instance. However, when you dynamically scope a variable, its binding is inherited from the calling function, even if that function isn't "outside" this one. For example, function a() { int x = 2; b(); } function b() { print x; } In function b, x isn't even defined. However, some languages allow you to get along with this. They simply assume that if function a calls function b, then function a has to define x before it can call function b. Then function b will inherit the same variable. That's called "dynamic" scoping. It's called "dynamic" 1) Because it makes for a much more exciting practice problem and 2) Because it cannot be resolved when the code is compiled but must instead be figured out "on the fly" by the language runtime. I hope this helps. Quote Link to comment Share on other sites More sharing options...
randhawa Posted November 6, 2003 Share Posted November 6, 2003 AlbaLed - regarding the soln: pass by reference, Static scope prints 4 10 pass by reference, Dynamic scope prints 3 5 Shdn't it be the other way round... i.e Static Dynamic ???? I'm not sure.... Quote Link to comment Share on other sites More sharing options...
AlbaLed Posted November 6, 2003 Share Posted November 6, 2003 nope I think it is correct as is Let's see, trace the execution for pass-by-reference, static scoping int x = 1; procedure P(i) { (i points to global x = 1) int x = 1; i++; (i = global x = 2) Q(i); } (go to print) procedure Q(j) { (j points to global x = 2) j = j + x; ( j = globale x = 4) } P(x) (x is global x) print x (output is 4) I guess you can do the other call of P(x) by yourself Quote Link to comment Share on other sites More sharing options...
randhawa Posted November 6, 2003 Share Posted November 6, 2003 Thats what I think happens in dynamic... Actually I wasn't able to get the ans for dynamic binding (it seems that the global variable is not been changed in the ans you gave... why is that??) Quote Link to comment Share on other sites More sharing options...
AlbaLed Posted November 6, 2003 Share Posted November 6, 2003 Originally posted by randhawa Thats what I think happens in dynamic... Actually I wasn't able to get the ans for dynamic binding (it seems that the global variable is not been changed in the ans you gave... why is that??) Dynamic scope int x = 1; procedure P(i) { (i points to global x = 1) int x = 1; i++; (i = global x = 2) Q(i); } (go to print) procedure Q(j) { (j points to global x = 2) j = j + x; (x = x in P, therefore j = 2 + 1 = 3) } P(x) (x is global x) print x (output is 3) Quote Link to comment Share on other sites More sharing options...
a7medy_1001 Posted December 24, 2008 Share Posted December 24, 2008 procedure Main is x : Integer ; procedure sub 1 is begin -- of sub 1 put(x) ; end ; --- of sub 1 procedure sub 2 is x : Integer ; begin -- of sub 2 x := 10 ; sub1 end ; --- of sub 2 begin -- of Main x:= 5; sub2 end -- of main what value of x is printed in sub 1 ?? ( assumeing ststic scoping rules ) what value of x is printed in sub 1 ?? ( assuming dynamic scoping rules ) can anybody help me please??????? thank you ... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.