Solidity provides different scopes based on the place of variable declaration in a contract.

  • Global Scope(State) variable
  • Local Variable
  • Global Inbuilt Variable

Solidity State variable

variable declared in the contract in one of the scopes.

variable declared in the contract are called state variables

data is stored permanently in blockchain storage

pragma solidity ^0.6.0;
contract SolidityTest {
   uint stateData;      // State variable
   constructor() public {
      stateData = 40;   // Using State variable
   }
}

Notes:

  • These variables are updated or assigned initially or constructor or setter functions -State variables are stored in Blockchain data storage
  • It is an expensive gas operation
  • Blockchain storage is fixed not dynamic
  • Contract object Instance has state variables declared only in it

Local Variables

variables declared in functions are called local variables. The scope is limited to function and data of these variables are stored in Stack in EVM.

pragma solidity ^0.5.0;
contract VariablesContractTest {
   /*
   *State variables are declared inside a contract ie global scope
   */
   uint age; // State variable
   constructor() public {
      age = 10;   
   }
   function add() public view returns(uint){
      // local variables are declared and accessed inside a function
      uint localVariable1 = 12; // local variable
      uint localVariable2 = 2; // local variable
      uint sum = localVariable1 + localVariable2; // local variable
      return sum; // access the local variable
   }
}

Notes

  • These variables are local to the function and stored in a Stack in EVM
  • It is only accessible in declared function and the variable is not available to access outside a function
  • It is an inexpensive gas operation and does not cost gas
  • These variables are referred to as state variables that stored in the storage

As of now, Learned global and local custom variables Next, learn Global predefined inbuilt scope variables in solidity

Global Inbuilt Variable

Solidity defines the Global scope of predefined variables and is accessible anywhere contract running in the blockchain.

It returns the block, transaction, and account information of the running blockchain node.

Global Variable Return Type Description
block.basefee uint
block.difficult uint returns block difficult
block.chainid uint returns Current block Chain ID
block.gaslimit uint returns Current block Gas Limit
block.number uint returns Current block number
block.timestamp uint returns Current block timestamp in seconds from Unix epoch time(1970-01-1
msg.data bytes It returns caller message data
msg.sender address It returns sender caller details
msg.sig bytes4 Returns first four bytes of an caller message data
msg.value uint Returns number wei sent
tx.gasprice uint returns gas price of current transaction
tx.origin address current transaction sender call chain details

All the above global inbuilt variables are used anywhere in a contract.

Here is an example code of how to use global variables in solidity contracts.

pragma solidity ^0.5.0;

contract GasCostCalculator{
   
   uint public gasCost;
      
   constructor() public {  
      gasCost = tx.gasprice;
   }
}