Table of contents
1.
Introduction
2.
Scope
3.
Semantic Rules for Scopes
3.1.
Static Scoping 
3.2.
Dynamic Scoping
4.
Static Vs. Dynamic Scoping 
5.
Static Storage Allocation
6.
Dynamic Storage Allocation
7.
Frequently Ask Questions
7.1.
Which scoping support in Perl?
7.2.
Define local scope.
7.3.
Define global scope.
7.4.
What are access modifiers, and what is their use?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Scope Information

Author Muskan Gupta
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Compiler Design

Introduction

The declaration of identifiers and the areas of a program where each identifier is authorized are described by scope information. Variables, functions, objects, and labels are examples of identifiers.

This blog will also teach about scope Information, scoping classes, and storage allocation. 

So, let's begin!

Scope

In the program region, the scope of a variable x, where x refers to its declaration. One of the main reasons for scoping is to keep variables separate in different program portions. Because there are a limited number of short variable names and programmers have similar naming tendencies, Every moderately sized program will utilize the same variable name in different contexts.

Also Read - Symbol Table Operations

Semantic Rules for Scopes

The following are the main rules for scopes: 

1. Use an identifier only if defined in the enclosing scope.

2. Do not declare the same kind of identifier with the same name in the same lexical scope more than once.

For learning about the scope of variables, visit scope of variable blog.

Scoping is generally divided into two types:

  1. Static Scoping
  2. Dynamic Scoping

 

Let's discuss them in detail.

Static Scoping 

Lexical scoping is another name for static scoping. A variable in this scope always refers to its top-level environment. This characteristic of the program text has nothing to do with the call stack at runtime. Static scoping makes it considerably easier to write modular code because a programmer can find out the scope by looking at the code. On the other hand, Dynamic Scope necessitates the programmer's anticipating all conceivable dynamic circumstances.

Variables are always statically (or lexically) scoped in most programming languages, including CC++, and Java. This means that the binding of a variable may be decided by program text and is independent of the runtime function call stack.

For example, the result of the following program is 10, indicating that the value produced by f() is independent of who calls it (such as g(), which has an x of 20). f() always returns the value of the global variable x.

Example

// A C program to demonstrate static scoping.
#include<stdio.h>
int a = 10;
 
// Called by g()
int f()
{
  return a;
}
 
// g() has its own variable named as x and calls f()
int g()
{
  int a = 20;
  return f();
}
 
int main()
{
  printf("%d", g());
  printf("\n");
  return 0;
}
You can also try this code with Online C Compiler
Run Code

 

Output 

10

 

The Compiler first searches the current block, global variables, and successively smaller scopes in static scoping.

Dynamic Scoping

With dynamic scope, a global identifier directs to the identifier associated with the most current environment and is unusual in modern languages. In technical terms, each identifier has a global stack of bindings, and the most current binding is explored for events of the identifier.

In another way, the Compiler successfully explores the current block and all calling functions first in dynamic scoping.

Example

// A C program to demonstrate dynamic scoping.

#include<stdio.h>
int a = 10;
 
// Called by g()
int f()
{
  return a;
}
int g()
{
  int a = 20;
  return f();
}
main()
{
  printf(g());
}
You can also try this code with Online C Compiler
Run Code

 

 Output 

20

 

This is a C program to demonstrate dynamic scoping.

Static Vs. Dynamic Scoping 

Static scoping is prevalent in most programming languages. Static scoping is easy to reason about and understand by looking at the code. By glancing at the text in the editor, we can see what variables are in the scope.

Dynamic scoping is concerned with how the code is executed rather than written. Each time a new function is called, a new scope is added to the stack.

Static Storage Allocation

The Compiler decides on storage allocation in static allocation by looking only at the program text. The Compiler allocates space for all variables (local and global) of all procedures at compile time.

There will be no overheads as it does not use stack/heap allocation. Variable access is fast since addresses are known at compile time. No recursion.

Dynamic Storage Allocation

The Compiler allocates space only for global variables at compile time in dynamic storage allocation decisions. Space for variables of procedures will be allocated at runtime. It uses

stack/heap allocation.

Variable access is slow (compared to static allocation) since addresses are accessed

through the stack/heap pointer. Recursion can be implemented.

Must Read Static Blocks In Java.

Frequently Ask Questions

Which scoping support in Perl?

Both dynamic and static scoping are supported in Perl. Perl's keyword "my" refers to a statically scoped local variable, whereas "local" refers to a dynamically scoped local variable.

Define local scope.

Variables defined in the current function are referred to as local scope in scope information. In its local Scope, a function will always be the first lookup for a variable name. Only the outside scopes are checked if they can't detect anything there.

Define global scope.

A global variable is defined outside of all of a program's functions. This means the global variable can be accessed inside or outside of all the functions in a program.  

What are access modifiers, and what is their use?

The access modifiers determine the accessibility or scope of a field, method, constructor, or class. We can alter the access level of domains, constructors, procedures, and courses using the access modifier. It can be used as the access level of fields, constructors, methods, and classes.

Conclusion

We hope that our blog enhances your knowledge regarding scope information. 

Recommended Reading:

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass