title: store reference variable in c++
body: <p>I have been struggling hard to find the difference between the following two pieces of code.</p>
  
  <p>This ...</p>
  
  <pre><code>int z=10;
  int y=&amp;z;
  </code></pre>
  
  <p>... is invalid whereas the following does not throw any error:</p>
  
  <pre><code>int&amp; foo()
  {
      int z=10;
      return z;
  }
  main()
  {
      int y=foo();
      cout&lt;&lt;y;
      return 0;
  }
  </code></pre>
  
  <p>When I tried to run the program it returned <code>y=10</code>.</p>
  
  <p>My question is:</p>
  
  <p>If <code>y</code> can store the reference of another variable using <code>foo()</code>, why not directly using <code>y=&amp;z</code>?</p>