Pascal & Object Pascal DISCUSSION

Returning a value from a Pascal function: function name, Result or Exit, and why no early return?

Started by nishenmahendran Pascal function resultResult variableExit with valueDelphi functionsundefined return value
4 replies 248 views 5 participants
Latest activity · 30 Sep 2026

Returning a value from a Pascal function: function name, Result or Exit, and why no early return?

nishenmahendran Pascal & Object Pascal Forum
#1

I wrote a search function and expected Result := i to work like return in C. The body is for i := 0 to High(A) do if A[i] = V then Result := i; Result := -1; and it returns -1 for every input, even when the value is in the array. Older tutorials assign to the function's own name instead of Result.

How does a Pascal function actually return its value, what is the difference between the two styles, and how do I leave a function early with a value?

Community replies 4

Re: Returning a value from a Pascal function: function name, Result or Exit, and why no early return?

#2

Assigning the result and leaving the function are two separate things in Pascal. Result := i only stores a value in the function's result variable; execution carries on with the next statement. Your loop therefore keeps running, and the final Result := -1 overwrites whatever was found. The function returns when it reaches its last end, with whatever the result variable holds at that moment.

The minimal fix is to set the default first and stop on a match: Result := -1; for i := 0 to High(A) do if A[i] = V then begin Result := i; Break; end;.

Re: Returning a value from a Pascal function: function name, Result or Exit, and why no early return?

#3

To return immediately, use Exit. Current Delphi and Free Pascal versions accept a value as an argument, so if A[i] = V then Exit(i); inside the loop, followed by Result := -1; after it, reads almost like the C version. Plain Exit; leaves with the result as it currently stands. Either way, any enclosing finally blocks still run before the function returns, so resources released there are safe.

Note that without the Break or Exit, a loop assigning Result := i on every match returns the last match, not the first: for the array 7, 3, 7 and V = 7 that is index 2 instead of 0.

Re: Returning a value from a Pascal function: function name, Result or Exit, and why no early return?

#4

The two styles are equivalent ways of naming the same result variable. Standard Pascal only has the first: inside function Square(x: Integer): Integer you write Square := x * x. The name may appear only on the left of an assignment, because on the right it is a recursive call. That makes accumulating awkward; you need an extra local variable. Result, introduced by Delphi, is an ordinary variable that you can read as well as write, so Result := Result + A[i] works.

In Free Pascal, Result is available in the objfpc and delphi modes, which Lazarus uses, but not in the plain default mode unless it is enabled with a mode switch.

Re: Returning a value from a Pascal function: function name, Result or Exit, and why no early return?

#5

A function that ends without assigning its result on some path returns an undefined value, not zero. Both compilers warn that the return value might be undefined; treat that warning as an error. Setting a default on the first line is the simplest discipline.

There is a subtler version with strings, dynamic arrays and interfaces. For these managed types the result is passed internally as a hidden variable supplied by the caller and is not cleared on entry, so a function that does Result := Result + 'x' without first assigning Result := '' can return 'x', then 'xx', then 'xxx' when called in a loop. Always initialise Result before appending to it.

TEP COMMUNITY