title: Why do we use functions that return a data structure in C++?
body: <p>I have been learning C++ and came across a function, but the return type was a vector.</p>
  <p>Here is the code:</p>
  <pre><code>vector&lt;Name&gt; inputNames() {
      ifstream fin(&quot;names.txt&quot;);
      string word;
      vector&lt;Name&gt; namelist;
  
      while (!fin.eof()) {
          Name name;
          fin &gt;&gt; name.first_name;
          fin &gt;&gt; name.last_name;
          namelist.push_back(name);
      }
  
      return namelist;
  }
  </code></pre>
  <p><code>name</code> is part of a struct defined as:</p>
  <pre><code>struct Name {
      string first_name;
      string last_name;
  
      bool operator&lt;(const Name&amp; d) const {
          return last_name &gt; d.last_name;
      }
  
      void display() {
          cout &lt;&lt; first_name &lt;&lt; &quot; &quot; &lt;&lt; last_name &lt;&lt; endl;
      }
  };
  </code></pre>
  <p>What is the purpose of using <strong>vector&lt; Name&gt;inputName()</strong>? What is it doing?</p>
  <p>And why can I just not create a void function and pass a vector through it?</p>
  <p>I.e.:</p>
  <pre><code>void input(vector&lt;Name&gt;&amp;v){
      ifstream fin(&quot;names.txt&quot;);
      string word;
  
      while (!fin.eof()) {
          Name name;
          fin &gt;&gt; name.first_name;
          fin &gt;&gt; name.last_name;
          v.push_back(name);
      }
  }
  </code></pre>