Question

Consider the following code: int a:=10 //global int b:=12 //global proc F a:= a-b proc P...

Consider the following code:

int a:=10   //global
int b:=12   //global
proc F  
  a:= a-b
proc P (M:proc)  
    int a:=2 
    M()
proc K 
    int b:=3   
    P(F)
K()                  //main program
print(a)          //built in function

 

a- what does this code print if it uses dynamic scoping and deep binding?

b- what does this code print if it uses dynamic scoping and shallow binding?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

Let's have a definition of deep binding and shallow binding first.

Deep binding:

Deep binding captures the environment at the point. and it takes the environment of the parent function.

Shallow binding:

Shallow binding takes the environment of the final calling function.

a)

int a:=10   //global
int b:=12   //global
proc F  
  a:= a-b
proc P (M:proc)  
    int a:=2 
    M()
proc K 
    int b:=3   
    P(F)
K()                  //main program
print(a)          //built in function

main method is where K() is called Now in the K method body b of int type is defined and initialized with the value 3.

Now F is called now a=a-b, here the value of a and b will be called from parent function which means the global value of a and b which is 10 and 12 respectively. now a= -2.

now the control is back to the main an print(a) will print -2.

b)

Here also

main method is where K() is called Now in the K method body b of int type is defined and initialized with the value 3.

Now F is called now a=a-b, here the value of a and b will be called from the function which called the function where we are at; this means the value of b will be taken from the calling function which is 3 and the value of a will be taken from the global value which is 10 now a= 7

now the control is back to the main an print(a) will print 7.

Hit the thumbs up if you liked the answer. :)

Add a comment
Know the answer?
Add Answer to:
Consider the following code: int a:=10 //global int b:=12 //global proc F a:= a-b proc P...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT