Reference Types

Data storage locations

Variables are declared with the words storage, memory or calldata to specify the location to save the data.

  • storage - variable is state variable (stored on blockchain)

  • memory - Variables are in memory and only exist while the function is running

  • calldata - A special data store containing the data passed to the function

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

contract DataLocations {
    uint256[] public arr; // in storage

    // You can return memory variables
    function g(uint256[] memory _arr) public returns (uint256[] memory) {
        // do something with memory array
    }

    function h(uint256[] calldata _arr) external {
        // do something with calldata array
    }

}

Array

Array is a combination of value elements in the same format, similar to list in python and array in Javascript.

Struct

Struct is a data format that programmers declare to gather many variables of different formats under one name for easy use in contract.

Last updated