17

Is there any guaranteed default value for the Result variable of a function, like 0, '' or nil? Or should Result always be initialised before use?

I have a function returning a string like this:

function Foo(): String
begin
    while {...} do
    Result := Result + 'boingbumtschak';
end;

It worked fine, but now I get some strings containing contents from a previous call to the function. When I add a Result := '' at the beginning, it is OK. When should I initialize the Result variable and when don't I have to? (strings, primitives, Class instances (nil))

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hansmaad
  • 18,417
  • 9
  • 53
  • 94
  • possible duplicate of [Which variables are initialized when in Delphi?](http://stackoverflow.com/questions/861045/which-variables-are-initialized-when-in-delphi) – Lieven Keersmaekers Mar 17 '11 at 09:03
  • possible duplicate of [Do I need to setLength a dynamic array on initialization?](http://stackoverflow.com/questions/5314918/do-i-need-to-setlength-a-dynamic-array-on-initialization) – Jeroen Wiert Pluimers Mar 17 '11 at 10:42
  • The single most ugly thing is that the compiler neither does complain about the unitialized result, nor is it treated and auto-initialized like an `out`. Instead, it is more kind of a `var` and hence gives you the ability (if we want to call it that way) to do Dumb Things™ with it. – JensG Dec 14 '15 at 23:23

3 Answers3

23

A function return value of type string is actually treated by the compiler as an implicit var parameter. When the function begins execution, the Result variable contains whatever is in the local variable to which the return value will subsequently be assigned.

Accordingly, you should always initialize function return values. This advice holds not only for strings, but for all data types.

This issue was discussed just yesterday here on Stack Overflow:

Do I need to setLength a dynamic array on initialization?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • @LievenKeersmaekers This seems to be the answer you are talking of: https://stackoverflow.com/a/861178/2932052 – Wolf Sep 06 '21 at 10:29
8

If the function exits without assigning a value to Result or the function name, then the function's return value is undefined.

see Delphi Reference > Procedures and Functions > Function Declarations

splash
  • 13,037
  • 1
  • 44
  • 67
2

I don't know what it's like in Delphi, but I always initialize variables to a sane value before I perform operations on them (even if that sane value is null, which it might very well be in some situations). Many times it's unneeded, but in those instances, I count on the compiler or JITter to optimize the assignment out if it wants to, rather than relying on potentially undocumented language semantics or compiler implementation details. Maybe it's my background in C, which in and of itself essentially guarantees nothing about initial values, but it feels worthwhile to spend the extra one line of code (at most) in order to get clearer code. By explicitly assigning a value before you start working on the variable, you establish a clear contract with whoever is reading the code; they can trust that their idea of what the starting value of the variable is, actually holds.

As for this particular question, though; isn't Result supposed to be function-local in scope? I would be very surprised if such a variable, even though special, kept values from previous invocations of the function.

user
  • 6,897
  • 8
  • 43
  • 79
  • Yes, it looks like I should be! Another reason to add to the list for my preference to always initialize variables before using them. – user Mar 17 '11 at 09:36
  • *always initialize variables* -- This is in general good style. But there are languages that provide implicit initialization, so there are chances that you write inefficient code, see for example (most) C++ classes with their constructors where you have a well-supported by-value option. – Wolf Sep 06 '21 at 10:07