Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

State where you use dynamic scoping and why you use it here. If you need to impl

ID: 3809494 • Letter: S

Question

State where you use dynamic scoping and why you use it here. If you need to implement it using static scoping instead of dynamic scoping, what will you do? You do not need to implement it, just describe your idea clearly. Now try to summarize your understanding about dynamic scoping.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

int b = 5;

int foo()

{

   int a = b + 5;

   return a;

}

int bar()

{

   int b = 2;

   return foo();

}

int main()

{

   foo(); // returns 10

   bar(); // returns 7

   return 0;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

int b = 5;

int foo()

{

   int a = b + 5;

   return a;

}

int bar()

{

   int b = 2;

   return foo();

}

int main()

{

   foo(); // returns 10

   bar(); // returns 7

   return 0;

}

Explanation / Answer

Dynamic refers to change. If you are using dynamic scope for a variable then it keep changing the variable value refers to portion of run time (time period during execution) and is known as dynamic scope.

Perl and Logo allow dynamic scoping though their local keyword.

C, C++, C#, and JavaScript all use static scoping.

In static scoping, Variables can be declared in that scope, and aren't visible from the outside.

Static resolution can be determined at compile time, and is also known as early binding, while dynamic resolution can in general only be determined at run time, and thus is known as late binding.

Example :-

Static scoping------>

int b = 5;

int foo()

{

   int a = b + 5;

   return a;

}

int bar()

{

   int b = 2;

   return foo();

}

int main()

{

   foo(); // returns 10

   bar(); // returns 10

   return 0;

}

Dynamic scoping----------->

int b = 5;

int foo()

{

   int a = b + 5;

   return a;

}

int bar()

{

   int b = 2;

   return foo();

}

int main()

{

   foo(); // returns 10

   bar(); // returns 7

   return 0;

}

So dynamic scoping means that when a symbol is referenced, the compiler/interpreter will walk up the symbol-table stack to find the correct instance of the variable to use.

This can be use when writing software.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote