Search code examples
memoryviewblockchainethereumsolidity

Why memory keyword is mentioned in view function in solidity


  function getAllWaves() public view returns (Wave[] memory) {
        return waves;
    }

I was going through _buildspace tutorial and found this code. I had previously read that memeroy keyword is used in solidity to specify that variable should be stored in memeroy(instead of state), so that we can destroy it after function call. But why memory is mentioned in return part of this view function since it just returns a value.

I am newbie in solidity so any articles relating to this also will help. Thanks


Solution

  • All reference type variables (including an array) need to have their data location specified.

    The EVM is not able to return directly from storage, as the storage location modifier acts like a pointer, and it would be useless to return just a pointer instead of the actual value.

    So it loads the value of waves array from storage to memory, and then returns it from memory.