title: Implications of not assigning the return value of a function to a variable
body: <p>I'm going through some introductory C++ exercises, one of which is: What's going to be on the screen after running this code:</p>
  
  <pre><code>int&amp; testMethod (int&amp; a){
  
      int c = a+a;
      return c;
  };
  
  int main() {
      int z = 5;
      int* y = new int (testMethod(z));
      int r = 25;
      testMethod(r);
      std::cout&lt;&lt;*y;
      return 0;
  }
  </code></pre>
  
  <p>My questions are:</p>
  
  <ol>
  <li>Am I right that this is an example of UB as the value returned by <code>testMethod</code> on the second call is not getting assigned to a variable?</li>
  <li>Is it true that the return value, although not assigned to a variable, might still be recovered but it depends?</li>
  </ol>
  
  <p>For No 2, I'm just trying to confirm my understanding of how the stack works, which is as follows. When a function returns a value and the value gets assigned to a variable, the result of the calculations is firstly stored on the stack as <code>retval</code> at some memory location and then gets assigned to the variable, i.e. written to another memory location. The stack pointer then moves up (again, from what I understand, the 'top' of the stack is actually its 'bottom' as the pointer moves from the largest address to the smallest). But <code>retval</code> is still there for some time until it gets overwritten by another piece of data (which might happen almost instantly).</p>
  
  <p>This second question arose when I was looking for an answer to the first question on SO and found <a href="https://stackoverflow.com/questions/3198356/function-return-not-assigned-to-a-variable">this</a> thread, as the two top (by votes) posts answer differently.</p>