String results and the weird stuff

There is an interesting particularity to the Delphi compiler. It relates to Strings and Dynamic Arrays. Consider the following two routines:

function DoSomeThing() : String;
  Result := Result + ' World';

procedure DoSomeThing2(var Result: String);
  Result := Result + ' World';

What is the difference between these two routines? Nothing except the syntax. In both cases Result is actually a var parameter sent to the function. So all the functions that return a string or a dynamic array are treated as procedures that have plus one parameter — the result variable.

Now consider this code:

procedure MyFunction();
  X: String;
  X := 'Hello';
  X := DoSomeThing();

You would think that X will become ” World” when in fact the result is “Hello World”. And this is correct if we consider that in fact this call “X := DoSomeThing()” will be translated to “DoSomeThing(X)“.

So the next time you’re writing a function that returns a string or a dynamic array be sure to initialize the Result to an empty string (or a nil for arrays).

One final note: In case of the main begin … end. block this behaviour changes. The compiler will actually allocate another local (invisible) variable that will receive the result and the copy that hidden variable to X so the result will be ” World”. This is probably due to optimizations.

You May Also Like

About the Author: Alexandru Ciobanu

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.