I am learning Ethereum dev in solidity and trying to run a simple HelloWorld program but run into the following error:
Data location must be "memory" or "calldata" for return parameter in function, but none was given.
Here is my code:
pragma solidity ^0.8.5;
contract HelloWorld {
string private helloMessage = "Hello world";
function getHelloMessage() public view returns (string){
return helloMessage;
}
}
Calldata is a non-modifiable, non-persistent area where function arguments are stored. It behaves mostly like memory. Any variable defined as calldata cannot be modifiable. In simple terms this means that you cannot change the value of the state of that variable.
Strings in Solidity is a reference type of data type which stores the location of the data instead of directly storing the data into the variable. They are dynamic arrays that store a set of characters that can consist of numbers, special characters, spaces, and alphabets.
Now, with the new versions of solidity you can return a struct also by using memory keyword after writing struct name in return type.
You need to return string memory
instead of string
.
Example:
function getHelloMessage() public view returns (string memory) {
return helloMessage;
}
The memory
keyword is the variable data location.
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