Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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";     } } 
like image 889
venus12 Avatar asked Oct 06 '19 15:10

venus12


People also ask

What is data location in solidity?

In Solidity, all of the state variables and local variables have a data location specified by default, or you can explicitly define a chosen data location. These data locations are called memory and storage. You can override the default data location by explicitly specifying along with the declaration of the variable.

What is memory keyword in solidity?

memory is a keyword used to store data for the execution of a contract. It holds functions argument data and is wiped after execution. storage can be seen as the default solidity data storage. It holds data persistently and consumes more gas.

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.

How do you use structs in solidity?

To define a structure struct keyword is used, which creates a new data type. For accessing any element of the structure, 'dot operator' is used, which separates the struct variable and the element we wish to access. To define the variable of structure data type structure name is used.


1 Answers

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

like image 73
Dushyanth Kumar Reddy Avatar answered Sep 18 '22 17:09

Dushyanth Kumar Reddy