Return string in solidity 0.5.0. Data location must be "memory" for return parameter in function
Asked Answered
C

2

6

How to return string in 0.5.0 solidity compiler version?

contract Test {
    string public text = 'show me';
    function  test() public view returns (string) {
        return text;
    }
}

I got error message:

TypeError: Data location must be "memory" for return parameter in function, but none was given.
Caprification answered 23/11, 2018 at 13:26 Comment(0)
C
11

Just need to add memory after string, like this:

function test() public view returns (string memory) {

Another changes: https://solidity.readthedocs.io/en/v0.5.0/050-breaking-changes.html#interoperability

Caprification answered 23/11, 2018 at 13:33 Comment(0)
X
1
//The version I have used is 0.5.2

pragma solidity ^0.5.2;

contract Inbox{


string public message;

//**Constructor** must be defined using “constructor” keyword

//**In version 0.5.0 or above** it is **mandatory to use “memory” keyword** so as to 
//**explicitly mention the data location**

//you are free to remove the keyword and try for yourself

 constructor (string memory initialMessage) public{
 message=initialMessage;
 }

 function setMessage(string memory newMessage)public{
 message=newMessage;

 }

 function getMessage()public view returns(string memory){
 return message;
 }}
Xanthin answered 7/1, 2019 at 6:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.