Open In App

Solidity – Variables

Improve
Improve
Like Article
Like
Save
Share
Report

A Variable is basically a placeholder for the data which can be manipulated at runtime. Variables allow users to retrieve and change the stored information. 

Rules For Naming Variables

1. A variable name should not match with reserved keywords.

2. Variable names must start with a letter or an underscore (_), and may contain letters from “a to z” or “A to Z” or digits from “0 to 9” and characters also.

Example:

Geeks123, geeks, _123geeks are valid variable names 
123geeks, $Geeks, 12_geeks are invalid variable names

3. The name of variables are case sensitive i.e.

Example:

Geeks123 and geeks123 are different variables

Declaration of Variables

In Solidity declaration of variables is a little bit different, to declare a variable the user has to specify the data type first followed by access modifier. 

Syntax:

<type> <access modifier> <variable name> ; 

Example:

int public int_var; 

Types of Variables

Solidity is a statically typed language i.e. each declared variable always has a default value based on its data type, which means there is no concept of ‘null’ or ‘undefined’. Solidity supports three types of variables: 

1. State Variables: Values of these variables are permanently stored in the contract storage. Each function has its own scope, and state variables should always be defined outside of that scope.

Example: In the below example. the contract Solidity_var_Test initializes the values of an unsigned integer state variable using a constructor.
 

Solidity




// Solidity program to 
// demonstrate state 
// variables
pragma solidity ^0.5.0;
  
// Creating a contract
contract Solidity_var_Test {
     
   // Declaring a state variable
   uint8 public state_var;      
  
   // Defining a constructor
   constructor() public {
      state_var = 16;   
   }
}


Output : 

State Variables Output

2. Local Variable: Values of these variables are present till the function executes and it cannot be accessed outside that function. This type of variable is usually used to store temporary values.

Example: In the below example, the contract Solidity_var_Test defines a function to declare and initialize the local variables and return the sum of the two local variables.

Solidity




// Solidity program to demonstrate
// local variables
pragma solidity ^0.5.0;
  
// Creating a contract
contract Solidity_var_Test {
  
   // Defining function to show the declaration and
   // scope of local variables
   function getResult() public view returns(uint){
       
      // Initializing local variables
      uint local_var1 = 1; 
      uint local_var2 = 2;
      uint result = local_var1 + local_var2;
       
      // Access the local variable
      return result; 
   }
}


Output : 
 

Local Variables Output

3. Global Variables: These are some special variables that can be used globally and give information about the transactions and blockChain properties. Some of the global variables are listed below :

Variable Return value
blockhash(uint blockNumber) returns (bytes32) Hash of a given block, works for only 256 most recent transactions excluding current blocks
block.coinbase (address payable) Address of current blocks miner
block.difficulty (uint) The difficulty of the current block
block.gaslimit (uint) Gaslimit of the current block
block.number (uint) Block number of the current block
block.timestamp (uint) The timestamp of the current block as seconds since Unix epoch
gasleft() returns (uint256) Amount of gas left
msg.data (bytes calldata) Complete call data of block
msg.sender (address payable) The sender of message i.e. current caller
msg.sig (bytes4) First four bytes of call data i.e. function identifier
msg.value (uint) Amount of Wei sent with a message
now (uint) The timestamp of the current block
gasleft() returns (uint256) Amount of gas left
tx.gasprice (uint) Price of gas for the transaction
tx.origin (address payable) Transaction sender

Example: In the below example, the contact Test uses the msg.sender variable to access the address of the person deploying the contract.

Solidity




// Solidity program to 
// show Global variables
pragma solidity ^0.5.0;
  
// Creating a contract
contract Test { 
    
  // Defining a variable
  address public admin;
    
  // Creating a constructor to
  // use Global variable
  constructor() public {    
    admin = msg.sender;  
  }
 }


Output:

Global variable



Last Updated : 11 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads