[Solidity] How to receive multiple return values?

This section describes how to receive multiple return values in a function in Solidity.

Syntax

To receive multiple return values in a function in Solidity, do the following.

Describe the data type for the return value in returns, and describe each return value in round brackets (()) in returns.

Note that multiple return values are returned as a single array.

function function name access modifier state modifier returns (return type 1, return type 2, return type 3, ..., return type n) {
    return (return value 1, return value 2, return value 3, ..., return value n);
}

Sample Code

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

contract Test {
    
    function getMultipleReturnValues() public pure returns(uint, string memory, address) {
        return (1, "solidityrun", address(0)); // [1, "solidityrun", 0x0000000000000000000000000000000000000000] array is returned
    }

}

When writing Solidity code, you will often want to return multiple values from a function. Learn how to write a function to return multiple values (return) so that you can deal with such cases.

タイトルとURLをコピーしました