Question

What are static scope rules and dynamic scope rules for a variable? Explain difference between them...

What are static scope rules and dynamic scope rules for a variable? Explain difference between them using an example.

Homework Answers

Answer #1

Answer:------------

Static Scoping:
Static scoping is also called lexical scoping. In this scoping a variable always refers to its top level environment. This is a property of the program text and unrelated to the run time call stack. Static scoping also makes it much easier to make a modular code as programmer can figure out the scope just by looking at the code.

For example, output for the below program is 100, i.e., the value returned by fun() is not dependent on who is calling it (Like run() calls it and has a x with value 43). fun() always returns the value of global variable x.
// A C program to demonstrate static scoping.

#include<stdio.h>
int x = 100;
int fun()
{
return x;
}
int run()
{
int x = 20;
return fun();
}
  
int main()
{
printf("%d", run());
printf("\n");
return 0;
}

Output :
10

In simpler terms, in static scoping the compiler first searches in the current block, then in the surrounding blocks successively and finally in the global variables.

Dynamic Scoping:
With dynamic scope, a global identifier refers to the identifier associated with the most recent environment, and is uncommon in modern languages. In technical terms, this means that each identifier has a global stack of bindings and the occurrence of a identifier is searched in the most recent binding.

#include<stdio.h>
int x = 100;
int fun()
{
return x;
}
int run()
{
int x = 20;
return fun();
}
  
main()
{
printf(run());
}

Output:
20

In simpler terms, in dynamic scoping the compiler first searches the current block and then successively all the calling functions.

Know the answer?
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
Similar Questions
Explain each of the following concepts in 2 sentences or less Static scope Dynamic scope Static...
Explain each of the following concepts in 2 sentences or less Static scope Dynamic scope Static binding Dynamic binding Call-by-value Call-by-reference Call-by-name
Explain the difference between static data consolidation and dynamic data consolidation as it relates to multiple...
Explain the difference between static data consolidation and dynamic data consolidation as it relates to multiple workbooks or worksheets. Provide an example of each type that you would use in a career that relates to your field of study. Be specific as to why you would use each type in each example.
What is difference between static and kinetic friction forces? Give one example of each of them....
What is difference between static and kinetic friction forces? Give one example of each of them. Why Static friction force is larger than kinetic friction force?
Question 2: What are the major differences between a static model and a dynamic model when...
Question 2: What are the major differences between a static model and a dynamic model when it comes to valuation of natural resources? In your opinion, which model is more suitable for valuating depletable natural resources? And why? What are the main factors that could affect the valuation of natural resources? Use example(s) if necessary.
What is the difference between kinetic friction and static friction? Describe the difference using the impending...
What is the difference between kinetic friction and static friction? Describe the difference using the impending motion and static equilibrium terms.
What is the difference between Economies of Scale and Economies of Scope?
What is the difference between Economies of Scale and Economies of Scope?
Explain the major difference between static and nonstatic sampling. What are the three main parts of...
Explain the major difference between static and nonstatic sampling. What are the three main parts of statical and non statical methods
What is the difference between a static and flexible budget?
What is the difference between a static and flexible budget?
Answer following questions briefly Explain difference between Disjoint and Overlap constraint rules for super-type and sub-type...
Answer following questions briefly Explain difference between Disjoint and Overlap constraint rules for super-type and sub-type entity with example of each. Explain in brief: Primary Key, Candidate Key, and Surrogate Key Explain the difference between WHERE and HAVING clause of SQL
Define what a scope of inference is for statistics. What is the difference between a t-value...
Define what a scope of inference is for statistics. What is the difference between a t-value and a sample statistic. Thank you.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT