James Bachini

Multiple Return Values | Solidity Tips & Examples

multiple return values solidity

In Solidity we can multiple return values from a function. This enables us to efficiently move data about within the application flow of a smart contract.

function returnVars() internal pure returns(uint,uint,uint) {
    uint var1 = 1;
    uint var2 = 2;
    uint var3 = 3;
    return (var1,var2,var3);
}

In this example we are returning three variables enclosed within brackets. Note that the data types of these variables are defined at the top of the function in the return section.

We can retrieve data using the same bracket structure

function getVar() external pure returns(uint) {
    (uint a, uint b, uint c) = returnVars();
    (,uint d,) = returnVars();
    return b;
}

This getVar() function makes two example calls to returnVars(); the first stores the returned unsigned intergers into varablies a, b & c. The second call ignores the first and last value and stores the second in variable d.

There is a full example of this code in the Solidity snippets github repo: https://github.com/jamesbachini/Solidity-Snippets/blob/main/contracts/MultipleReturns.sol

multiple return values solidity

Get The Blockchain Sector Newsletter, binge the YouTube channel and connect with me on Twitter

The Blockchain Sector newsletter goes out a few times a month when there is breaking news or interesting developments to discuss. All the content I produce is free, if you’d like to help please share this content on social media.

Thank you.

James Bachini

Disclaimer: Not a financial advisor, not financial advice. The content I create is to document my journey and for educational and entertainment purposes only. It is not under any circumstances investment advice. I am not an investment or trading professional and am learning myself while still making plenty of mistakes along the way. Any code published is experimental and not production ready to be used for financial transactions. Do your own research and do not play with funds you do not want to lose.