Solidity 101

1. Hello Web3 (三行代码)
2. 值类型
3. 函数
4. 函数输出
5. 变量数据存储和作用域
6. 引用类型
7. 映射类型
8. 变量初始值
9. 常数
10. 控制流
11. 构造函数和修饰器
12. 事件
13. 继承
14. 抽象合约和接口
15. 异常
.
Function Output (return/returns)

Recently, I have been revisiting Solidity, consolidating the finer details, and writing "WTF Solidity" tutorials for newbies.

Twitter: @0xAA_Science | @WTFAcademy_

Community: Discord|Wechat|Website wtf.academy

Codes and tutorials are open source on GitHub: github.com/AmazingAng/WTF-Solidity


In this chapter, we will introduce the Solidity function output, including returning multiple values, named returns, and reading full or part of return values using destructuring assignments.

Return values (return and returns)

There are two keywords related to function output: return and returns:

  • returns is added after the function name to declare variable type and variable name;
  • return is used in the function body and returns desired variables.
// returning multiple variables
    function returnMultiple() public pure returns(uint256, bool, uint256[3] memory){
            return(1, true, [uint256(1),2,5]);
        }

In the above code, the returnMultiple() function has multiple outputs: returns (uint256, bool, uint256[3] memory) , and then we specify the return variables/values in the function body with return (1, true, [uint256 (1), 2,5]) .

Named returns

We can indicate the name of the return variables in returns, so that solidity automatically initializes these variables, and automatically returns the values of these functions without adding the return keyword.

// named returns
    function returnNamed() public pure returns(uint256 _number, bool _bool, uint256[3] memory _array){
        _number = 2;
        _bool = false; 
        _array = [uint256(3),2,1];
    }

In the above code, we declare the return variable type and variable name with returns (uint256 _number, bool _bool, uint256[3] memory _array) . Thus, we only need to assign values to the variable _number, _bool and _arrayin the function body, and they will automatically return.

Of course, you can also return variables with return keyword in named returns:

// Named return, still support return
    function returnNamed2() public pure returns(uint256 _number, bool _bool, uint256[3] memory _array){
        return(1, true, [uint256(1),2,5]);
    }

Destructuring assignments

Solidity internally allows tuple types, i.e. a list of objects of potentially different types whose number is a constant at compile-time. The tuples can be used to return multiple values at the same time.

  • Variables declared with type and assigned from the returned tuple, not all elements have to be specified (but the number must match):
uint256 _number;
        bool _bool;
        uint256[3] memory _array;
        (_number, _bool, _array) = returnNamed();
  • Assign part of return values: Components can be left out. In the following code, we only assign the return value _bool2, but not _ number and _array:
(, _bool2, ) = returnNamed();

Verify on Remix

  • Deploy the contract, and check the return values of the functions.

Summary

In this section, we introduced function return values return and returns, including returning multiple variables, named returns, and reading full or part of return values using destructuring assignments.

PreviousNext