title: Should variable be initialized before call to function?
body: <p>When I call Functions to get a value, I usually initialize varible, in case function fails or doesn't return anything and I want to avoid dealing with uninitialized variable. I do the same for string, integer or any other type.</p>
  
  <p>Example for integer variables:</p>
  
  <pre><code>vPropValue := 0;
  vPropValue := GetPropValue(vObject,'Height');
  
  IF vPropValue &gt; 0 Then
  ...
  </code></pre>
  
  <p>this it the most common how I use it.</p>
  
  <p>I know I could use:</p>
  
  <pre><code>If GetPropValue(vObject,'Height') &gt; 0 Then
  ...
  </code></pre>
  
  <p>but with first example I avoid multiple calls to function, if I need result again later in the code.</p>
  
  <p>Same for string (even though i know local strings are initialized to empty string, while integers are not an can hold any value)</p>
  
  <pre><code>vName := '';
  vName := GetObjectName(vObject,'ObjectName');
  
  IF Trim(vPropStrValue) &lt;&gt; '' Then
  ...
  </code></pre>
  
  <p>I know I could take steps to avoid duplicate value assignment,like making sure Function returns 0 if everything fails. But I have 100s of functions and I can't rely I never made a mistake how functions handle everything and I'm sure some don't return 0, if everything fails.</p>
  
  <p><strong>I'm trying to understand why this is not desirable practice and how to best avoid it.</strong></p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here is example where function doesn't return proper value or 0:</p>
  
  <pre><code>function GetValue(vType:integer):integer;
  begin
     if vType=1 then
      Result:=100
     else if (vType&gt;2) and (vType&lt;=9) then
       Result:=200;
  
  end;
  
  procedure TForm1.Button1Click(Sender: TObject);
  var vValue:integer;
  begin
  
    vValue:=GetValue(11);
    Button1.Caption:=IntToStr(vValue);
  
  end;
  </code></pre>
  
  <p>In this case the value returned from function is some random number.</p>
  
  <p>In this case the initialization appears to be valid approach. <strong>OR NOT?</strong></p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>As David pointed out in his answer, correct, there was a warning</p>
  
  <pre><code>[dcc32 Warning] Unit1.pas(33): W1035 Return value of function 'GetValue' might be undefined
  </code></pre>
  
  <p>but, I ignored it, for no reason, just didn't look there. As it let me compile it, I thought it was OK. So, I did look for the warning and I 'fixed' quite a few functions that had similar issue, because of all IFs Result might not have been defined.</p>
  
  <p><strong>EDIT 3 &amp; CONCLUSION:</strong></p>
  
  <p>I hope it adds to the scope of question and explanation: </p>
  
  <p>Maybe an example of another twist that I use in most of my Functions, would also explain why I thought my initialization of variable was needed, is that I wasn't sure my Functions would behave correctly all the times, especially in case of nested function. Mots of them still are set like this:</p>
  
  <pre><code>function GetProperty(vType:integer):integer;
  begin
    Try
      if vType = 99 then
        Result:=GetDifferentProperty(vType)// &lt;-- Call to another Function, that could return whatever...
      else
      begin
         if vType=1 then
            Result:=100
         else if (vType&gt;2) and (vType&lt;=9) then
           Result:=200;
      end;
    except
    end;
  end;
  </code></pre>
  
  <p>Now I'm addressing these <code>Try Except End;</code> but some functions are 10 years old and to expect them to work 100%, based on my experience back then, is not something to rely on.</p>
  
  <p>As the only developer on this project, I assume I should trust my functions (and he rest of my code), but I can't imagine in multiple developers environment that all function are set up properly.</p>
  
  <p><strong>So my conclusion</strong>: since I didn't take care of the basics - properly designed Functions, I need to have all these checks (variable initialization, <code>Try Except</code> lines..) and probably some other unneccessary stuff.</p>