×

Lexical Scope and Closure

Lexical Scope :

Lexical scope is a very important term in any programming language. It defines the scope of the variable based on the block it is contained inside. According to the lexical scope, the scope of the variable does not exist when the control is out of the block in which the variable was present. Dart is a lexically scoped language; therefore, we can find the scope of the variable by tracing the curly braces.

Consider the following example that explains the concept of lexical scope.

Program
// Declaring a global variable whose scope is the whole program
// Therefore, this variable can be used anywhere in the program in any
// function
var global = 5 ; 
void main( ) 
{
  // Declaring a local variable inside main( ). This variable can be 
  // accessed only within this function
  var inside_main = 6 ;
  print( " Value of global variable is : $global " ) ;
  print( " Value of variable inside main( ) is : $inside_main " ) ;
  
  void func_1( ) 
  {
    // Declaring a local variable inside func_1( ). This variable can be 
    // accessed only within this function
    var inside_geeksforgeeks = 7 ;
  
    void func_2( ) 
    {
      // Declaring a local variable inside func_2( ). This variable can be 
      // accessed only within this function
      var inside_geek = 8 ;
    }
  }
}

Output :

Value of global variable is : 5 
Value of variable inside main( ) is : 6

The above code explains the concept of the scope of the variable inside the functions in a program and how their scope ends outside the curly braces.

Lexical Closures :

In programming languages, lexical scope of a variable in a function can be implemented using lexical closure also called the closure or function closure. The implementation includes a function object that has access to variables in its lexical scope, even when the function is used outside the scope.

Consider the following example that explains the concept of lexical closure :

Program :

// definition of the function that adds the number
Function func1( num add ) 
{
  return ( num i ) => add + i ;
}
void main( ) 
{
  // creating an object ' obj1' that calls the func1( ) function to add 5
  var obj1 = func1( 5 ) ;
  
  // creating an object ' obj1' that calls the func1( ) function to add 5
  var obj2 = func1( 10 ) ;
  
  // passing the values in the object
  // printing the value of obj1 after adding 1 and 5
  print( " Value of first object : " ) ;
  print( obj1( 1 ) ) ;
  
  // printing the value of obj1 after adding 1 and 10
  print( " Value of Second object : " ) ;
  print( obj2( 1 ) ) ;
}

Output :

Value of first object : 
6
Value of Second object : 
11

Related Topics

Dart Equality and Relational Operators

Dart provides the functionality of checking the relationship between the values or values within the variables. These operators return the resultant value as Boolean, i.e., either ‘true’ or ‘false’. Some important points...

3 minutes read.

Dart Bitwise and Shift Operators

The Bitwise operators are the operators that perform different operations bit by bit on the value of the two operands. List of Bitwise Operators in Dart Sr. Operators Description 1. & ( Binary AND ) It returns...

2 minutes read.

Soundness in Dart

What is soundness? Soundness ensures that the program never comes across any invalid state that may lead to abrupt termination of the program. As its name suggests it ensures perfect type...

4 minutes read.

Dart Miscellaneous types

Some Other Data types in Dart Apart from the data types studied so far, we have three other data types also which are as follows: NeverDynamicVoid Let us understand each one in detail, Never...

3 minutes read.

Dart Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.

Dart Generics

Dart Generics is similar to Dart collections, which are used to store homogeneous data. As we have discussed in Dart's features, it is a language with types being optional. By default,...

4 minutes read.

Dart Loops

Looping refers to repeating or reusing the same lines of code repeatedly until a particular test condition evaluates to true. It is also known as iterating.  It is effectively used when...

4 minutes read.

Dart Operators Precedence and Associativity

Precedence of operators determines the order in which the operators are evaluated if they are grouped together in a sentence. If two operators share an operand then the one with...

5 minutes read.

Dart Function

A Dart function is a collective line of code that are targeted to perform a specialised task. Such lines of code (function) can be reused whenever we need to perform...

6 minutes read.

Dart Concurrency

Dart concurrency allows us to run multiple or multiple components of a system at once. It issues several commands at once. Dart provides Isolates as a tool for performing compliance...

2 minutes read.

Dart Switch Case Statement

In the case of if-else statements, we prefer not to use the if-else ladder when there are many test conditions to be evaluated. In such a situation, it is preferable...

4 minutes read.

Dart Break and Continue

Loop statements are used to change the normal sequence of flow of the program. Dart supports two types of loop control statements: Break StatementContinue Statement Break Statement : The ‘ break ’ statement is...

4 minutes read.

main function in Dart

The main( ) function is a predefined method in Dart that is also known as the entry-point of the program. The compiler begins the execution only when it comes across...

3 minutes read.

Dart - Control Flow Statements

The control flow statements are used to define the control on the flow of the program. Dart programs are executed in sequential order i.e., the order in which the programming...

1 minute read.

Dart this Keyword

The ' this ' keyword, similar to that in Java and C#, refers to an object of the class using it. It points to the current object of the class,...

5 minutes read.

Dart Miscellaneous Operators

Apart from the operators we studied so far, Dart provides some more operators which are as follows : Conditional OperatorCascade Notations Conditional Operators These are the operators that help in evaluating the expression...

1 minute read.

Metadata in Dart

Metadata is often referred to as the data about the data. It is a  piece of data about a basic piece of data. In the case of a Dart program,...

2 minutes read.

Dart Maps

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

5 minutes read.

Dart Comments

Comments are the statements that are used to enhance the readability and understandability of the code. The compiler does not execute these lines. It simply ignores these comments when it scans...

2 minutes read.

Dart Packages

Every programming language has some in-built functions stored inside the header files that makes the task much easier for the programmer. The Dart Packages refer to the compilation of a...

4 minutes read.