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

时间:2013-06-19 01:55:46

标签: 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] = m2[i] then
    begin
      distinct := distinct+1;
      dash := i;
      if distinct > 1 then
          result:= -10;
    end;
end;

result := dash;
end;

1 个答案:

答案 0 :(得分:8)

永远不会分配该值,因为您在最后一行中将result的值设置为dash

您可以从

更改代码
  if distinct > 1 then
      result:= -10;

  if distinct > 1 then
      dash:= -10;