E2055
Delphi Programming Language
Severity: ModerateWhat Does This Error Mean?
E2055 means Delphi detected that your function can reach its end without setting a return value. If an if statement only sets the Result in one branch and the other branch falls through, the function exits with an undefined value. Fix it by ensuring every possible code path through the function assigns a value to Result before returning.
Affected Models
- Delphi 10.4 Sydney
- Delphi 11 Alexandria
- Delphi 12 Athens
- Embarcadero RAD Studio
- Free Pascal / Lazarus
Common Causes
- An if statement sets Result in the true branch but not the false branch
- A case statement does not cover all possible values and has no else clause that sets Result
- An early exit with Exit before Result is set in some branches
- A try...except block sets Result in the try block but not in the except handler
- Complex nested conditions where one combination of conditions leaves Result unset
How to Fix It
-
Set a default value for Result at the very top of the function, before any conditional logic.
Example: Result := False; or Result := -1; or Result := ''; — this ensures a valid value is always returned even if no branch sets it.
-
Review every if...then...else in the function and ensure both the then and else branches set Result.
If the else branch was omitted, either add it with a Result assignment or set a default at the top of the function.
-
For case statements, add an else clause that sets a default Result value.
Example: case Status of 1: Result := 'OK'; 2: Result := 'Error'; else Result := 'Unknown'; end;
-
Check every Exit call in the function — before each Exit, ensure Result has been assigned the correct value.
In modern Delphi you can use Exit(Value) to set the result and exit in one step: Exit(False);
-
For try...except blocks, add a Result assignment in the except handler if the try block assigns Result.
If an exception occurs before Result is set in the try block, the except handler runs and Result may still be uninitialized.
When to Call a Professional
This is a compiler error — no professional repair needed. Fix it by reading the error message and correcting the code. The Delphi IDE highlights the exact line causing the problem.
Frequently Asked Questions
What value does Delphi return if a function exits without setting Result?
Delphi does not guarantee any specific value — it is undefined behavior. The function will return whatever random data happens to be in that memory location at runtime. This can cause hard-to-reproduce bugs that only appear in certain conditions, which is why the compiler warns you.
Can I use the function name instead of Result to return a value in Delphi?
Yes — in Delphi you can assign to the function name as an alternative to Result: GetValue := 42 is equivalent to Result := 42. However, using Result is strongly preferred in modern Delphi because it is clearer and works consistently. Mixing function-name assignment and Result in the same function can cause confusing behavior.
Is E2055 always an error or can it be a warning?
In Delphi it is typically reported as an error that prevents compilation, not just a warning. This is intentional — returning an undefined value from a function is always a bug, not an acceptable condition. The correct fix is always to ensure every code path explicitly sets Result to a meaningful value.