title: Am I having Kotlin functions return a List correctly?
body: <p>I am new to Kotlin (coming from Delphi, which is object-oriented Pascal). I just want to ensure I am having functions return <code>List&lt;&gt;</code>s correctly:</p>
  <p>Making up an absurdly simple example here:</p>
  <pre><code>    fun firstTenInts(): List&lt;Int&gt; {
          val iList: MutableList&lt;Int&gt; = mutableListOf()
          for (i in 1..10)
              iList.add(i)
          return iList
      }
  </code></pre>
  <p>So, my thoughts/questions here are:</p>
  <ul>
  <li><p>Am I correct to use a <code>MutableList</code> within the function and simply return it (even though the function's type is <code>List</code>)?</p>
  </li>
  <li><p>Do I <em>need</em> to create a local <code>MutableList</code> variable? Do I <em>need</em> any local &quot;list&quot; variable? I am used to (again, Delphi) doing something like:</p>
  </li>
  </ul>
  <pre><code>    function firstTenInts: TStringList;
      var
        i: integer;
      begin
      result.Clear;
      for i := 1 to 10 do
        result.Add(IntToStr(i));
      end;
  </code></pre>
  <p>which requires no &quot;new&quot; local variable. I simply &quot;work&quot; <code>result</code> which is very similar to Kotlin's <code>return</code> value except that it serves as a local variable of the function's type which can be &quot;worked&quot; throughout the function.</p>
  <ul>
  <li><p>Is there no way to manipulate a Kotlin function's <code>return</code> value other than with another (locally created) variable?</p>
  </li>
  <li><p>Finally, I can rest assured that any local variables I create are destroyed when the function ends even though I'm &quot;passing them back&quot; - correct?</p>
  </li>
  </ul>
  <p>P.S. I know this is an absurd way to create a <code>List</code> of 10 integers. I am using this only as a framework for the questions/issues I have detailed above. Assume that the returned <code>List</code> will be of unknown size.</p>
  <p>(Please do not suggest better ways of creating this list of integers; that is not what I am asking about).</p>