Solidity 代码错误:函数 `print_weekday` 没有返回值
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Demo { function print_weekday(uint _index) public pure returns (string memory) { string memory weekday = ""; if (_index == 1) { weekday = 'Monday'; } else if (_index == 2) { weekday = 'Tuesday'; } else if (_index == 3) { weekday = 'Wednesday'; } else if (_index == 4) { weekday = 'Thursday'; } else if (_index == 5) { weekday = 'Friday'; } else if (_index == 6) { weekday = 'Saturday'; } else if (_index == 7) { weekday = 'Sunday'; } return weekday; } }
在你的代码中,报错是因为在函数 print_weekday 的最后没有返回值。由于函数申明为 pure,它应该返回一个值,但是在所有的条件中都没有返回值。为了解决这个问题,你可以在函数的最后添加一个默认的返回值,例如 return weekday;。以下是修复后的代码:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Demo {
function print_weekday(uint _index) public pure returns (string memory) {
string memory weekday = "";
if (_index == 1) {
weekday = 'Monday';
} else if (_index == 2) {
weekday = 'Tuesday';
} else if (_index == 3) {
weekday = 'Wednesday';
} else if (_index == 4) {
weekday = 'Thursday';
} else if (_index == 5) {
weekday = 'Friday';
} else if (_index == 6) {
weekday = 'Saturday';
} else if (_index == 7) {
weekday = 'Sunday';
}
return weekday;
}
}
注意我还修正了你的条件中的一些拼写错误,并修改了星期几的名称。
原文地址: https://www.cveoy.top/t/topic/pf1o 著作权归作者所有。请勿转载和采集!