如何在Delphi函数中设置不同的结果值?

如何在Delphi函数中设置不同的结果值?,delphi,function,Delphi,Function,我正在编写这个函数,对于我设置result:=-10的那一行,编译器会给我一个警告,告诉我这样的值永远不会被忽略。我的逻辑有问题吗 function combine (m1, m2 : string) : integer; var dash : integer; distinct : integer; i : integer; begin distinct := 0; dash := -1; for i := 0 to Length(m1)-1 do begin if m1[i] =

我正在编写这个函数,对于我设置
result:=-10
的那一行,编译器会给我一个警告,告诉我这样的值永远不会被忽略。我的逻辑有问题吗

function combine (m1, m2 : string) : integer;
var
dash : integer;
distinct : integer;
i : integer;

begin
distinct := 0;
dash := -1;

for i := 0 to Length(m1)-1 do
begin
    if m1[i] = m2[i] then
    begin
      distinct := distinct+1;
      dash := i;
      if distinct > 1 then
          result:= -10;
    end;
end;

result := dash;
end;

由于您在最后一行中将
结果的值设置为
破折号
,因此不会指定该值

您可以从更改代码

  if distinct > 1 then
      result:= -10;


我想我可以将该行中的设置结果设置为-1作为返回值,您需要执行
exit结果
不会中断Delphi中的函数,如果我想退出,结果值设置为-10,根据您的Delphi版本,您可以编写
如果distinct>1,则开始结果:=-10;出口结束或<代码>如果不同>1,则退出(-10)请记住:结果只是设置函数的值,而不是退出方法。缺少返回语句以及函数结果是通过var参数而不是通过值返回的事实,是Delphi中的一个重大设计错误。
  if distinct > 1 then
      dash:= -10;