Open In App

Solidity – Variable Scope

Last Updated : 26 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Variable scope is an essential concept in Solidity, the programming language for Ethereum smart contracts. Solidity has various types of variables with different scopes, such as local, state, and global variables.

Local Variable

Local variables are declared within a function and are only accessible within that function. Their scope is limited, and their lifetime ends when the function execution is completed.

Syntax:

<type> <variable_name>;

Below is the Solidity program to implement the Local variable:

Solidity




// Solidity program to implement 
// local variable
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
  
contract LocalVariableExample {
  function getDouble(uint value) public pure returns (uint) 
  {
    uint doubleValue = value * 2;
    return doubleValue;
  }
}


Output: Calling the getDouble function with a value of 10 will return 20.

Local Variable

 

State Variable

State variables are declared at the contract level and represent the contract’s state on the blockchain. They are stored on the Ethereum network and are accessible within the entire contract.

Syntax:

<type> <variable_name>;

Below is the Solidity program to implement the State variable:

Solidity




// Solidity program to implement
// State variable
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
  
contract StateVariableExample {
  uint public count = 0;
  function increment() public 
  {
    count += 1;
  }
}


Output: Calling the increment function will increase the value of the count by 1. The value of the count can be accessed using the count public getter function generated by Solidity.

State Variable

 

Global Variable

Global variables are special variables provided by the Solidity language. They are available throughout the contract and provide information about the blockchain, transaction, and execution context.

Examples of Global Variables:

  • block.timestamp (current block timestamp)
  • msg.sender (address of the sender of the current function call)
  • msg.value (amount of ether sent in the current function call)

Below is the Solidity program to implement the Global variable:

Solidity




// Solidity program to implement 
// Global variable
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
  
contract GlobalVariableExample {
  event SenderAndValue(address sender, uint value);
  function getSenderAndValue() public payable 
  {
    address sender = msg.sender;
    uint value = msg.value;
    emit SenderAndValue(sender, value);
  }
}


Output: Calling the getSenderAndValue function will return the sender’s address and the ether value sent in the current function call.

Global Variable

 

Variable Scope

There are three types of variable scopes in Solidity:

1. Public

Public variables are accessible from within the contract and can be accessed from external contracts as well. Solidity automatically generates a getter function for public state variables.

Syntax: 

<type> public <variable_name>;

2. Private

Private variables are only accessible within the contract they are defined in. They are not accessible from derived contracts or external contracts.

Syntax:

<type> private <variable_name>;

3. Internal

Internal variables are accessible within the contract they are defined in and derived from contracts. They are not accessible from external contracts.

Syntax:

<type> internal <variable_name>;

Below is the Solidity program to implement the variable scope:

Solidity




// Solidity program to implement 
// the variable scope 
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
  
contract VariableScopeExample {
  uint public publicVar = 1;
  uint private privateVar = 2;
  uint internal internalVar = 3;
  
  function getPrivateVar() public view returns (uint) 
  {
    return privateVar;
  }
}
  
contract DerivedContract is VariableScopeExample {
  function getInternalVar() public view returns (uint) 
  {
    return internalVar;
  }
}


Output: The publicVar can be accessed using the automatically generated getter function publicVar(). The privateVar can be accessed using the getPrivateVar() function from within the VariableScopeExample contract. The internalVar can be accessed by the getInternalVar() function in the derived contract DerivedContract.

Variable Scope

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads