Are return values required in Solidity?

Quick question.

If I set some return parameters for a function do I ALWAYS have to provide them in a return? Will there be an error if no return values are supplied?

And on the other side, if values ARE returned by a function and there is no variable to catch them in some circumstances, is that acceptable as well if I don’t always want to catch them, or does it throw?

1 Like

Hi @CryptoEatsTheWorld,

I tried the following in Remix and only got one compiler warning (that doStuffReturn could be pure) or errors with Solidity 0.6.12.

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

contract MyContract {
    
    function doStuffNoReturn() public returns (bool) {
    }
    
    function doStuffReturn() public returns (bool) {
        return true;
    }
}

contract MyOtherContract {
    
    event DoneStuff(bool done);
    
    function doStuffNoReturn() public {
        MyContract myContract = new MyContract();
        
        myContract.doStuffNoReturn();

        bool done = myContract.doStuffNoReturn();
        
        emit DoneStuff(done);
    }
    
    function doStuffReturn() public {
        MyContract myContract = new MyContract();
        
        myContract.doStuffReturn();

        bool done = myContract.doStuffReturn();
        
        emit DoneStuff(done);
    }
}

There doesn't appear to be a compiler error but I assume this has potential to cause issues if code calling this function checks the return value and potentially does something based on the value.

Trying the contract above, I get a value of false.

I assume this is not an issue.

I couldn't find anything specific in the documentation: https://docs.soliditylang.org/en/v0.7.5/contracts.html#return-variables

Thanks. I ended up just making unique functions for my needs here, rather than trying to reuse certain functions with varying parameter needs. So I’m not sure if it would have worked or not, but I went with the safe solutions for now

Chris

1 Like