Available types of variables in solidity

  1. Local Variable
  2. Global Variable

State Variable

Variables which are mentioned and stored within the contract. In other words, values are permanently stored in a contract storage.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract VariablesInSol{
address ownerAddress; //State variable
constructor() {
ownerAddress=msg.sender; //using State variable
}
function getOwnerAddress() public view returns(address){
return ownerAddress; //return State variable
}
}

Local Variable

Local variables are that which use within the functions.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract VariablesInSol{
function getWalletBalance(address _address) public view returns(uint){
uint walletBalance = _address.balance; //Local variable
return walletBalance;
}
}

Global Variable

There are spacial variables which exist in global workspace and provide information about the blockchain and transactions.

  • msg: it represents messages.
  • block: it returns data about block.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract VariablesInSol{
function globalVariables() public view returns(address, uint, uint){
address _address = msg.sender; //global variable
uint timestamp = block.timestamp; //global variable
uint blockNumber = block.number; //global variable
return (_address, timestamp, blockNumber);
}
}
Gist url for access full code (click here)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store