Available types of variables in solidity

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

State Variable

// 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

// 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

  • tx: it represents 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
Zinzuvadiya Meet

Zinzuvadiya Meet

Hey there! I’m a mobile app developer who passionate about creating websites & applications through flutter as well as provides firebase services to clients.