Solidity Tutorial

Types of Variables in Solidity – State Variable, Local Variable & Global Variable

Share on twitter
Share on linkedin
Share on whatsapp
Share on facebook
Share on telegram

Contents

Types of Variables in Solidity

The Solidity programming language supports three types of variables as follows –

  1. State Variable
  2. Local Variable
  3. Global Variable

Let us understand these variables in more detail.

Solidity State Variable

In Solidity, the state variables are declared inside a contract but outside the functions. The state variables are stored permanently on the contract storage of the Ethereum blockchain. Since it uses contract storage, we have to pay the gas fees to use state variables. Hence we should be careful to use the number of state variables in the contract, because the more the state variables, the more gas fees will be applicable.

The memory allocated to the state variable is statically assigned, and cannot be changed during the lifetime of the contract.

Scope of State Variable

Inside the contract, the state variables can be accessed by any function directly.

At external levels, the state variables can have the following visibility types –

  1. Public – The state variables are available for any external call. The getter function is implicitly created by the compiler to read its value.
  2. Internal – The state variables are not accessible from any external calls. They can be accessed only from within the contract or from the contracts that are deriving it. If not specified the default scope of the state variable is Internal.
  3. Private – The state variables are neither accessible from any external calls nor from any other child contracts that are deriving it. They are only accessible from the functions inside the contract.

Initialization of State Variable

The state variables can be initialized in three ways –

1. Initializing during Declaration

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

contract DemoContract 
{
    uint public storedData = 25;

}

 

2. Initializing in Constructor 

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

contract DemoContract 
{
    uint public storedData;
    
    constructor () public
    {
       storedData = 25;
    }

}

 

3. Initializing in Function

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

contract DemoContract 
{
    uint public storedData;
    
  function set() public 
    {
        storedData = 25;      
    }

}

 

Solidity Local Variable

In Solidity, the local variables are those variables that are defined inside the functions. The scope of the local variable is within the function only and it is stored in memory due to which it is not permanently persistent. Since they are not stored on the Ethereum blockchain it does not incur gas fees.

Generally, the local variables are used inside function for holding the values for some calculation purposes.

Scope of Local Variable

The local variable is only visible inside the function in which it is declared. It cannot be accessed by any other external function or contract.

Initialization of Local Variable

The local variable can be initialized in the following ways –

1.  Initialization during Declaration

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract DemoContract 
{
        
  function calculate() pure public returns(uint)
    {
       // local variable
        uint amount = 200;
     
        return amount;
    }
}

 

2.  Initialization after Declaration

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

contract DemoContract 
{
        
  function calculate() pure public returns(uint)
    {
       // local variable
        uint amount;

        amount = 200;      

        return amount;
    }

}

 

State Variable vs Local Variable in Solidity

  • In a contract, the state variables are declared outside the function whereas the local variable is declared inside the function.
  • The state variables are stored permanently in the blockchain whereas the local variables are stored in the memory and are not persistent.
  • The state variables require a gas fee but the local variables do not need gas.
  • The state variable may have visibility of public, internal, or private. The local variable is visible only inside the function.

 

Solidity Global Variable

In Solidity, there are some special global variables available that give details about the Ethereum blockchain and the transactions that are happening on it. There are plenty of global variables whose list can be found in Solidity documentation.

Let us see the use of the Global variable in the example below.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

contract DemoContract 
{
        
  function calculate() view public returns(uint,uint,uint,uint)
    {
        return (block.number, block.difficulty,block.timestamp, tx.gasprice);
    }

}

Output

uint256: 17

uint256: 71762765929000

uint256: 1648302622

uint256: 1