Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solidity: Data location must be "memory" or "calldata" for return parameter in function

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;
  }
}
like image 267
Yash Jain Avatar asked Jun 21 '21 19:06

Yash Jain


People also ask

What is Calldata in solidity?

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.

What is string memory in solidity?

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.

How do I return a struct in solidity?

Now, with the new versions of solidity you can return a struct also by using memory keyword after writing struct name in return type.


1 Answers

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.

like image 118
Petr Hejda Avatar answered Sep 27 '22 16:09

Petr Hejda