Day 4

Day 04 - Variables and Scopes

2:30 AM, February 04, 2023

Solidity

Mohammad Mudassir


Variables

Solidity supports three types of variables -

1- State Variables - Variables whose values are permanently stored in a contract storage.

2- Local Variables - Variables whose values are present till function is executing.

3- Global Variables - Special variables exists in the global namespace used to get information about the blockchain.

State Variables

A state variable is a variable that is permanently stored in the contract's storage. State variables are used to keep track of the contract's state and can be accessed and modified by any function in the contract.

pragma solidity ^0.8.0;

contract MyContract {
  uint public myStateVariable;

  function setMyStateVariable(uint newValue) public {
    myStateVariable = newValue;
  }

  function getMyStateVariable() public view returns (uint) {
    return myStateVariable;
  }
}

In this contract, `myStateVariable` is a public state variable of type `uint` (an unsigned integer). The public keyword creates a getter function that can be used to read the value of the state variable from outside the contract. The setMyStateVariable function can be used to set the value of the state variable.

Local Variables

A local variable is a variable that is declared and used inside a function or a block of code. Local variables are used to store temporary values that are needed only within the scope of the function or block.

pragma solidity ^0.8.0;

contract MyContract {
  function add(uint a, uint b) public pure returns (uint) {
    uint result = a + b;
    return result;
  }
}

In this example, the `add` function takes two uint arguments, a and b, and returns their sum as a uint. The result variable is declared as a local variable inside the function and is used to store the sum of a and b. The function then returns the value of result.

Global Variables

A global variable is a variable that is available to all functions in a contract. Global variables are declared outside of any function, and their value is visible and can be modified by any function in the contract.

pragma solidity ^0.8.0;

contract MyContract {
  uint public myPublicVariable; // a public global variable
  address private owner; // a private global variable

  constructor() {
    owner = msg.sender; // set the value of the owner variable to the address of the contract creator
  }

  function setMyPublicVariable(uint newValue) public {
    myPublicVariable = newValue;
  }

  function getOwner() public view returns (address) {
    return owner;
  }
}

In this example, `myPublicVariable` is a public global variable of type uint that can be accessed and modified by any function in the contract. owner is a private global variable of type address that can only be accessed and modified by functions in the contract that have the private or internal visibility specifier.

Variable Scope

Variable scope refers to the portion of a contract where a variable can be accessed and used. Solidity has four types of variable visibility: public, private, internal, and external, which define the scope of a variable.

pragma solidity ^0.8.0;

contract MyContract {
    uint public publicVariable; // public variable
    uint private privateVariable; // private variable
    uint internal internalVariable; // internal variable
    uint external externalVariable; // external variable
    
    function myFunction() public {
        uint localVariable = 10; // local variable
        
        // access public variable
        publicVariable = 100;
        
        // access private variable
        privateVariable = 200;
        
        // access internal variable
        internalVariable = 300;
        
        // access external variable
        externalVariable = 400;
        
        // access local variable
        localVariable = 20;
    }
}

In this example, publicVariable is a public variable, which means it can be accessed and modified by any contract or external account.

PrivateVariable is a private variable, which means it can only be accessed and modified within the contract where it is defined.

InternalVariable is an internal variable, which means it can be accessed and modified by any contract that inherits from the contract where it is defined.

ExternalVariable is an external variable, which means it can only be accessed and modified by external accounts.