0
pragma solidity ^0.5.16;

contract mycontract{
    uint256 public a;
 
    function foo(string calldata _name) external {
        string memory names;
        names = _name; 
        getname(names);
    }

    function getname(string memory nameget) internal view returns(string memory){
        return nameget;
        
    }

    function setinteger(uint256 _a) public{
        a = _a;    
    } 
}

What's the issue with above code? When foo is called it should return the string but it does not.

0

There are two problems with the code:

  1. foo as a function doesn't return anything. If you want to return something you have to declare that it will return it.

  2. foo is not a view or pure function. Off-chain we can easily obtain the return value from view and pure functions but it is not easy for non-constant function. You can read this answer for more details: What is the difference between a transaction and a call?.

In case foo doesn't modify storage you can declare it as a view and make it to return a string.

    function foo(string calldata _name) external view returns (string memory) {
        string memory names;
        names = _name; 
        return getname(names);
    }

In case foo does modify storage you cannot declare it as a view nor pure, one solution is to emit an event with the value as parameter, see the linked question for the details.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.