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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With