Solidity – How to Return Multiple Values from a Function in Solidity

contract-developmentsolidity

We're having requirement to get failure message from require validation. I've searched a lot for the same and found that Remix is displaying messages, however web3 is not.

In order to get messages, we have created internal function which returns Boolean result and a string message.

function validateDoc(
    bytes32 _a,
    string _b,    
    uint256 _c
    ) internal view 
    returns(bool, string){

    if(condition 1){
        return(false, "failure message1"); 

    }else if(condition 2){
        return(false, "failure message2"); 
    }
    }else if(condition 3){
        return(false, "failure message3"); 
    }        
    }else if(condition 4){
        return(false, "failure message4"); 
    }        
}  

Further, I'm calling it from another function:

function Main(
    bytes32 _a,
    string _b,
    uint256 _c    
) public returns(bool, string) {

    validateDoc(_a, _b, _c); // TODO: return bool result and message.

    // Other code 
    // ...
    return (true, "success message");                
} 

But the issue is, how to return both result of validateDoc in Main function?

Best Answer

function Main(
    bytes32 _a,
    string _b,
    uint256 _c    
) public returns(bool _success, string _message) {

    (_success, _message) = validateDoc(_a, _b, _c); // TODO: return bool result and message.

    if(_success) {
        // Other code 
        // ...
    }
    return (_success, _message);                
} 

If i understood you correctly it should look like this.

Related Topic