TypeError: Data location must be "memory" or "calldata" for parameter in function, but none was given

Viewed 1129

I am trying the following code but got that error.

If I add memory inside functions params I got new error:

TypeError: Data location can only be specified for array, struct or mapping types, but "memory" was given.

pragma solidity 0.8.7;

 mapping (string => uint) wallet;

function saveWalletData(uint _qty , string _name) public{
    wallet[_name] = _qty;
}

function consultarWallet(string _name) public view returns(uint){
    return wallet[_name];
}
1 Answers

string is a reference type in Solidity. For all reference types, you need to specify their data location (docs).

In this case, you can use calldata for both, because you're not modifying value of the _name variable.

function saveWalletData(uint _qty , string calldata _name) public{
    wallet[_name] = _qty;
}

function consultarWallet(string calldata _name) public view returns(uint){
    return wallet[_name];
}

If you wanted to modify the value in memory, you'd need to use memory.

Related