Gives Error.
data location must be a memory for the return parameter in the function, but none was given.
In the below function I am getting error.
function getCitizen()public returns(address[]){
return citizenArray;
}
the smart contract that I have tried so far.
pragma solidity ^0.5.1;
contract Citizen{
struct Citizens{
uint age;
string fName;
string lName;
}
mapping(address => Citizens) citizenMap;
address [] citizenArray;
function setCitizen(address _address,uint _age,string memory _fName,string memory _lName) public{
//creating the object of the structure in solidity
Citizens storage citizen=citizenMap[_address];
citizen.age=_age;
citizen.fName=_fName;
citizen.lName=_lName;
citizenArray.push(_address) -1;
}
function getCitizen(address _address) public pure returns(uint,string memory ,string memory ){
return(citizenMap[_address].age,citizenMap[_address].fName,citizenMap[_address].lName);
}
function getCitizenAddress()public returns(address[]){
return citizenArray;
}
}
thanks in advance for the help.
In Solidity, from any type of function, you can return multiple values. You can do this by using the return(value1, value2, ...) statement or by directly assigning the value to the variable name that's used in the returns() statement.
Solidity function returns an array of string, it returns correctly in remix, but falsely in web3. js, as the first one of the array is an empty one, and missied the last one.
It make sense, as you are returning the storage
array of address you cannot return it as it is, because it will try to return the actual address of citizenArray
in the contract storage. You can send the array by making it in memory
. Like this.
function getCitizenAddress()public view returns( address [] memory){
return citizenArray;
}
Once you put it as memory
, you will get the warning for this which will state that as you are not changing any state in the function, you should mark it view
, I already did that in the above code.
Lastly, when you resolved this error, you will get another error in this function:
function getCitizen(address _address) public pure returns(uint,string memory ,string memory ){
return(citizenMap[_address].age,citizenMap[_address].fName,citizenMap[_address].lName);
}
This error is because you mark this function as pure
. There is a little but very important difference between pure
and view
.
view
means you cannot change the state of the contract in that function.pure
means you cannot change the state in the function and not even can read the state or storage variables.In the above function of getCitizen
you are actually doing read operations in your return
statement. You can fix this by just putting view
instead of pure
. Like So:
function getCitizen(address _address) public view returns(uint,string memory ,string memory ){
return(citizenMap[_address].age,citizenMap[_address].fName,citizenMap[_address].lName);
}
I hope it will resolve all your issues. Thanks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With