Search code examples
blockchainethereumsolidity

Data location must be "memory" for return parameter in function, but none was given


I tried solidity example like as above in remix, solidity version > 0.5.0 But I am getting this error now. What is the way to solve this error?

contract MyContract {
    string value;

    function get() public view returns (string) {
        return value;
    }

    function set(string _value) public {
        value = _value;
    }

    constructor() public {
        value = "myValue";
    }
}

Solution

  • You should add memory keyword for string parameter, which was introduced in solidity version 0.5.0

    As per the documentation:

    Explicit data location for all variables of struct, array or mapping types is now mandatory. This is also applied to function parameters and return variables. For example, change uint[] x = m_x to uint[] storage x = m_x, and function f(uint[][] x) to function f(uint[][] memory x) where memory is the data location and might be replaced by storage or calldata accordingly. Note that external functions require parameters with a data location of calldata.

    Corrected code

    contract MyContract {
        string value;
    
        function get() public view returns (string memory) {
            return value;
        }
    
        function set(string memory _value) public {
            value = _value;
        }
    
        constructor() public {
            value = "myValue";
        }
    }
    

    Refer to official documentation on breaking changes made in version 0.5.0