【发布时间】:2021-05-20 22:33:46
【问题描述】:
如果我有函数return_num(),我需要return 一些值,但如果我switch 插入值x,我确定default 将return 值@987654328 @,如果我输入其他内容...是否有必要在函数末尾添加另一个 return 值 1?
编译器不会抱怨。
int return_num(int x) {
switch (x) {
case 0: return 10;
case 1: return 25;
case 2: return 33;
default: return 1;
}
// <--- necessary return?
}
【问题讨论】:
-
就个人而言,我会简单地删除
default案例并将return移到末尾:int return_num(int x) { switch (x) { case 0: return 10; case 1: return 25; case 2: return 33; } return 1; }