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