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.

Ismael
  • 30,570
  • 21
  • 53
  • 96
user61878
  • 67
  • 1
  • 3

1 Answers1

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.

Ismael
  • 30,570
  • 21
  • 53
  • 96