# Variables, Consts & Immutable

### Variables

There are 3 types of variables in Solidity:

* `local`
  * Declared inside a function.
  * Not stored on the blockchain.
* `state`
  * Declared outside a function.
  * Stored on the blockchain.
* `global`
  * Provides information about the blockchain.

### Example - Variables

* <https://solidity-by-example.org/variables/>

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

contract Variables {
    // State variables are stored on the blockchain.
    string public text = "Hello";
    uint public num = 123;

    function doSomething() public {
        // Local variables are not saved to the blockchain.
        uint i = 456;

        // Here are some global variables
        uint timestamp = block.timestamp; // Current block timestamp
        address sender = msg.sender; // address of the caller
    }
}
```

### Constants

* Constants are variables that cannot be modified
* Their value is hard coded into the bytecode of the contract
* Using constants can save gas cost

### Example - Constants

* <https://solidity-by-example.org/constants/>

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

contract Constants {
    // coding convention to uppercase constant variables
    address public constant MY_ADDRESS = 0x777788889999AaAAbBbbCcccddDdeeeEfFFfCcCc;
    uint public constant MY_UINT = 123;
}
```

### Immutable

* Immutable variables are like constants
* Values of immutable variables can be set inside the `constructor` but cannot be modified afterwards

### Example - Immutable

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

contract Immutable {
    // coding convention to uppercase constant variables
    address public immutable MY_ADDRESS;
    uint public immutable MY_UINT;

    constructor(uint _myUint) {
        MY_ADDRESS = msg.sender;
        MY_UINT = _myUint;
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.eridian.xyz/ethereum-dev/solidity-notes/variables-consts-and-immutable.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
