Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between Exit(1) or Result := 1; Exit in Delphi?

Tags:

delphi

In Delphi, you can Exit a function and give it a return value which is very similar to the return keyword in C/C++/Java/C# etc.

Exit(1);

But, I usually write something like this:

Result := 1;
Exit;

Any reasons to choose one over another?

like image 667
justyy Avatar asked Feb 07 '17 10:02

justyy


1 Answers

These two variants are semantically identical and you are free to choose between them.

If you need code to compile on older versions of the compiler which do not support the Exit(<value>) syntax then clearly you would have to avoid that variant.


Personally I avoid Exit(<value>) simply because I don't like there being two different ways to assign a return value. I hate functions like this:

function FindValue(Value: Integer): Integer;
var
  i: Integer;
begin
  for i := 0 to Count - 1 do
    if Items[i] = Value then
      Exit(i);
  Result := -1;
end;

Here we mix the two different approaches. Obviously we could opt to use Exit(<value>) at all times, but then I could not write that function like so:

function FindValue(Value: Integer): Integer;
begin
  for Result := 0 to Count - 1 do
    if Items[Result] = Value then
      Exit;
  Result := -1;
end;

In my opinion, this is one of those times where an enhancement has been added that introduces programmer choice but yields very little benefit. I would have preferred for Exit(<value>) not to have been introduced. However, as I said, these are my personal opinions and I'm sure other people have different views.

like image 196
David Heffernan Avatar answered Oct 04 '22 04:10

David Heffernan