Available types of variables in solidity

Solidity supports three types of variables.

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

Solidity is a statically typed language, which means that we have to be specified the state or local type variables during declaration. Each declared variable always have a default value based on its type. There is no concept of “undefined” or “null”.

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

In this example, we can access ownerAddress variable at anywhere within the contract,

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;
}
}

In this example, we can access walletBalance variable within browser, but we can not access it outer the function block.

Global Variable

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

Below I mention some global variables. For batter understanding I discussed it into three different categories:

  • tx: it represents transactions.
  • msg: it represents messages.
  • block: it returns data about block.

For more variables I provide here I provide official solidity documentation link(click here).

// 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);
}
}

On above example, I return three common global variables.

Below I added my gist file according to upper example for your easiness

Gist url for access full code (click here)

♥️️♥Thank you for reading this article. If you like it don’t forget to hit on 👏🏻 clap.

--

--

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

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