这篇文章给大家介绍Solidity中怎么利用returns函数接收多个值,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
函数返回多个值
// 函数可以返回多个值 function multipleReturns() returns(uint a, uint b, uint c) { return (1, 2, 3); }
同时接收所有返回值
function processMultipleReturns() { uint a; uint b; uint c; // 这样来做批量赋值: (a, b, c) = multipleReturns(); }
只接收部分返回值
// 或者如果我们只想返回其中一个变量: function getLastReturnValue() { uint c; // 可以对其他字段留空: (,,c) = multipleReturns(); }
项目中实际碰到问题
function multipleReturns() returns(string memory a, uint b, uint c) { return ("1", 2, 3); } function processMultipleReturns() { string memory a; uint b; uint c; // 这样来做批量赋值: (a, b, c) = multipleReturns(); }
mapping (uint256 => Tree) public dnaToTree; //通过 Dna 获取加密资产详情 function getTreeByDna(uint256 _Dna) public constant returns(string memory treeByDna_user , uint256 treeByDna_dna,string memory treeByDna_treeName, uint64 treeByDna_birthTime, uint256 treeByDna_needFooder, bytes32 treeByDna_status, bool treeByDna_isAdult) { return (dnaToTree[_Dna].user, dnaToTree[_Dna].dna, dnaToTree[_Dna].treeName, dnaToTree[_Dna].birthTime, dnaToTree[_Dna].needFooder ,dnaToTree[_Dna].status ,dnaToTree[_Dna].isAdult); } function getTreeByDna_d(uint256 _Dna) public constant { string memory treeByDna_user; uint256 treeByDna_dna; string memory treeByDna_treeName; uint64 treeByDna_birthTime; uint256 treeByDna_needFooder; bytes32 treeByDna_status; bool treeByDna_isAdult; (treeByDna_user, treeByDna_dna, treeByDna_treeName, treeByDna_birthTime, treeByDna_needFooder, treeByDna_status, treeByDna_isAdult) = getTreeByDna(_Dna); }
关于Solidity中怎么利用returns函数接收多个值就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。