×

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 super keyword

The ' super ' keyword is used to refer to the immediate parent class object of the currently in-use child class. Using this keyword, we can invoke the superclass (...

6 minutes read.

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 - extends, with and implements Keywords

The application development in Dart programming language using the Flutter framework, regularly experience different usage of the implements, extends and with keywords. Dart has full support for inheritance that is...

5 minutes read.

Dart Strings

Strings data type in Dart A Dart string is a sequence of characters with an encoding of UTF-16. The text associated with string data type is enclosed withing single (‘  ’)...

3 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 lists data type

The most important data type in any programming language is an ordered list of elements, an array. Dart lists resemble JavaScript array lists. Example,               var...

7 minutes read.

Dart Booleans

Booleans Dart has a data type named ‘bool’ to represent Boolean values : true and false, which are compile-time constants. ‘True’ or ‘False’ cannot be assigned with values 1 or 0. Example, bool...

1 minute 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 Single-Page Application Architecture

In a single page application architecture, the source code for a single web page loads the entire application. The responsibility of building the user interface and requesting data from the...

2 minutes read.

Dart Basic Program

Dart provides various convenient platforms to code and compile the program in Dart language. Some of them are: 1. Using IDE = Visual Studio code is a text editor with IDE....

3 minutes read.

Dart Types of Functions

Functions are the set of statements intended to perform a specific task. They accept some input from the user, perform the specified computation and generate the output. They are very...

4 minutes read.

C++ vs Dart

Difference between C++ and Dart C++ is an object-oriented programming language developed in 1980 by Bjarne Stroustrup. It has wide real-world applications with various in-built functions and a very vast library...

1 minute read.

Dart Anonymous Function

Dart functions are the user defined functions that are designed to perform specific task. The use case of the functions is that they can be directly called any number of...

3 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 Features

Dart is a fully-featured, object-oriented modern language with its roots in the programming language ‘Smalltalk’. It has some of its eminent features similar to the languages such as Java, C#...

4 minutes read.

Dart Symbols

Symbols data type in Dart A symbol is an object that is the representation of an operator or identifier in Dart. These are compile-time constants.  They are used in APIs that refer...

1 minute read.

Callable Classes in Dart

Just like the functions, we can also call the instances of classes in Dart. Such classes are also known as “callable ” classes. We need to use the “call( )”...

3 minutes read.

Dart Classes

Dart is an object – oriented programming language that supports all the object-oriented programming concepts such as classes, objects, inheritance, data abstraction and data encapsulation. A class can be defined as...

8 minutes read.

Assert in DART

In any programming language, resolving error is the most unwanted and tedious task. At times it becomes very difficult to find the error in the large code. Dart offers an...

3 minutes read.

Dart Future and Stream

Before understanding Future and Stream in Dart, we need first to get familiar with two terms : Synchronous and Asynchronous Programming.  In synchronous programming, a single operation is handled at a...

9 minutes read.