我这个delphi自定义函数存在什么问题?

来源:百度知道 编辑:UC知道 时间:2024/05/20 11:31:10
type
Trange=1..50;
function txtbackcoler(const colernum:Trange):integer;//自定义函数
begin
case colernum of
1..7: result:=1;
8..15: result:=2;
16..23: result:=3;
24..32: result:=4;
33..40: result:=5;
end;
end;
RUN后即出现[Warning] Unit1.pas(427): Return value of function 'txtbackcoler' might be undefined
我的函数返回值应该是定义了呀。请高人指点迷津!!

是这样,colernum 有可能在
1..7 8..15 16..23 24..32 33..40
这些数字范围外,编译器检测到了这个纰漏,就报警告了。
你给Result设置个默认值就不会了。

先Result := 0;
再case

或者加else

type
Trange=1..50;
end;

function txtbackcoler(const colernum:Trange):integer;//自定义函数
begin
case colernum of
1..7: result:=1;
8..15: result:=2;
16..23: result:=3;
24..32: result:=4;
33..40: result:=5;
end;

不追求完美就不必理会这个警告。
非要完美的话,在Case语句前加个返回值就行。

...
Result := 0;
Case colernum of
...

只是警告而已.
因为 如果colernum 是 41 你说你返回什么?
所以
case colernum of
1..7: result:=1;
8..15: result:=2;
16..23: result:=3;
24..32: result:=4;
33..40: result:=5;
else result:=0;
end;

先设默认值