Beginners Guide to Solidity Variables

Solidity, the cornerstone of Ethereum smart contract development, offers a robust system for variable declaration and management. In this guide, we'll delve deep into the three primary types of variables in Solidity and provide insights into their usage and characteristics.

graph TD A[Solidity Variables] B[State Variables] C[Local Variables] D[Global Variables] A --> B A --> C A --> D

State Variables: Permanent Storage in Contracts

State variables are the backbone of a Solidity contract. They are stored permanently on the Ethereum blockchain and represent the contract's state.

Characteristics of State Variables:

  • Permanence: Their values remain stored in the contract's storage, ensuring data persistence across transactions and function calls.
  • Default Values: Every state variable has a default value based on its type. For instance, an uninitialized uint will default to 0.

Example of State Variable:

Solidity
pragma solidity ^0.5.0;

contract SolidityTest {
   uint storedData;   // Declaration of State variable
   
   constructor() public {
      storedData = 10;   // Initialization of State variable
   }
}

Local Variables: Temporary Storage within Functions

Local variables are temporary and exist only within the function where they are declared. They are instrumental for intermediate calculations and data manipulation within functions.

Characteristics of Local Variables:

  • Scope: Their scope is limited to the function in which they are defined.
  • Function Parameters: Parameters passed to a function are treated as local variables.

Example of Local Variable:

Solidity
pragma solidity ^0.5.0;

contract SolidityTest {
   function getResult() public view returns(uint){
      uint a = 1; // Declaration and initialization of local variable
      uint b = 2;
      uint result = a + b;
      return result;   // Return the local variable value
   }
}

Global Variables: Insights into the Blockchain

Global variables provide a window into the Ethereum blockchain's properties. They offer information about the current block, transaction, and other essential blockchain metrics.

Notable Global Variables:

  • blockhash(uint blockNumber): Returns the hash of a given block.
  • block.coinbase: Address of the current block miner.
  • block.difficulty: Current block's difficulty metric.
  • block.gaslimit: Gas limit of the current block.
  • msg.sender: Address of the entity (person or contract) that called the current function.
  • now: Current block timestamp.

Best Practices for Naming Solidity Variables

When naming variables in Solidity, adhering to specific conventions ensures clarity and avoids potential errors:

  1. Avoid Reserved Keywords: Solidity has a set of reserved keywords that should not be used as variable names. For instance, break or boolean are not valid variable names.
  2. Start with a Letter or Underscore: Variable names should begin with a letter or an underscore. Hence, while _123test is valid, 123test is not.
  3. Case Sensitivity: Solidity treats Name and name as distinct variables. Always be consistent with your naming conventions.

FAQs

  1. What are State Variables in Solidity?
    • State variables are permanently stored on the Ethereum blockchain and represent a contract's state.
  2. How are Local Variables different from State Variables?
    • Local variables are temporary and exist only within the function where they are declared, whereas state variables are permanent.
  3. Can I use reserved keywords as variable names in Solidity?
    • No, using reserved keywords like break or boolean as variable names is not valid in Solidity.
  4. What information can Global Variables provide?
    • Global variables offer insights into the Ethereum blockchain's properties, such as the current block's miner address, difficulty, and gas limit.

Author