A value returned by a function in the smart contract cannot be captured as every function returns a hash.

Then, why write return statements in Solidity function?

For example,

event LogNewVoter(address sender, string name, bool isSuccess);

function addVoter(address _voter,string _name, bytes _voterImageHash) payable public returns(bool) {
    require(msg.sender == votingAdmin);
    Voter storage sender = voters[_voter];
        revert("This address is all ready registered as a voter!");
    voters[_voter].name = _name;
    voters[_voter].hasVoted = false;
    voters[_voter].isVoter = true;
    voters[_voter].voterImageHash = _voterImageHash;
    return true;

In above question, I can get whether the voter is registered successfully or not by using events. If I can not capture value returned by a function then why to write return statements in function?


In Solidity you can return values from functions to the user if these functions are declared view(/constant).

When they are not declared as view you can still get a return value by using .call() this will execute the call locally and not mine it onto the blockchain however.

Return values do play a big role as they can be returned to other functions even when the function is not view/constant. I'll give a real world example.

The following function creates a new 'bounty' on a bounty contract, it returns a bytes32 value.

  function createBounty(
    BountyInterface.Bounty storage _bounty,
    string _title,
    string _issueURL,
    string _reference,
    uint _deadline,
    address _issuer,
    uint _reward
  ) external returns (bytes32) {
      bytes32 _index = keccak256(abi.encodePacked(_issueURL));
      _bounty.bounties[_index].title = _title;
      _bounty.bounties[_index].issueURL = _issueURL;
      _bounty.bounties[_index].reference = _reference;
      _bounty.bounties[_index].timestamp = block.timestamp;
      _bounty.bounties[_index].deadline = _deadline;
      _bounty.bounties[_index].status = BountyInterface.statusOptions.ACTIVE;
      _bounty.bounties[_index].issuer = _issuer;
      _bounty.bounties[_index].reward = _reward;
      emit logCreateBounty(_issuer, msg.sender, _title, _reward);
      return _index;

The return value is then sent to this function on another contract. This will store the return value in an array so that the newly created bounty is associated with a user as well:

  function createBounty(address _group, string _title, string _issueURL, string _reference, uint _deadline, uint _reward) external {
    bytes32 _index = Group(_group).createBounty(_title, _issueURL, _reference, _deadline, msg.sender, _reward);
    People(ContractProvider(CMC).contracts("people-storage")).addBounty(_group, _index, msg.sender);

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.