Skip to content

Latest commit

 

History

History
43 lines (36 loc) · 1.25 KB

File metadata and controls

43 lines (36 loc) · 1.25 KB

101-04 函数的输出

返回值:return和returns

  1. returns: 跟在函数名之后,用于声明返回的变量类型和变量名
  2. returns: 函数主体中,返回指定变量
function ReturnMultiple() public pure returns(uint256, bool, uint256[3] memory){//声明了returnMultiple()函数
    return(1, true, [uint256(1),2,5]);//明确返回值
}

命名式返回

可以在returns中表明返回变量的名称,而无需再使用return声明

function ReturnsNamed() public pure returns(uint256 _num, bool _bool, uint256[3] memory _array){
        _num = 9;
        _bool = true;
        _array = [uint256(2),1,0];
    }

return在命名式返回中也依然可以使用

function ReturnNamed() public pure returns(uint256 _num1, bool _bool1, uint256[3] memory _array1){
        return(19, false, [uint256(9),8,7]);
    }

解构式赋值

Solidity 支持使用解构式赋值规则来读取函数的全部或部分返回值。

function ReadReturn() public pure{
        uint256 _num;
        bool _bool;
        bool _bool1;
        uint256[3] memory _array;
        (_num, _bool, _array) = ReturnsNamed();
        (,_bool1,) = ReturnNamed();//读取部分返回值,不读取的留空
    }