title: Should we use temporary variables for the returned values of functions?
body: <p>I thought about this: Is there a performance difference in these two practices:</p>
  
  <ol>
  <li>Store the return value of a function in a temporary variable than
  give that variable as a parameter to another function.</li>
  <li>Put the function into the other function.</li>
  </ol>
  
  <h2>Specification</h2>
  
  <p><em>Assuming all classes and functions are written correctly.</em></p>
  
  <h3>Case 1.</h3>
  
  <pre><code>ClassA a = function1();
  ClassB b = function2(a);
  function3(b);
  </code></pre>
  
  <h3>Case 2.</h3>
  
  <pre><code>function3(function2(function1()));
  </code></pre>
  
  <p>I know there aren't a big difference with only one run, but supposed that we could run this a lot of times in a loop, I created some tests.</p>
  
  <h2>Test</h2>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;ctime&gt;
  #include &lt;math.h&gt;
  using namespace std;
  
  int main()
  {
     clock_t start = clock();
     clock_t ends = clock();
  
     // Case 1.
     start = clock();
     for (int i=0; i&lt;10000000; i++)
     {
        double a = cos(1);
        double b = pow(a, 2);
        sqrt(b);
     }
     ends = clock();
     cout &lt;&lt; (double) (ends - start) / CLOCKS_PER_SEC &lt;&lt; endl;
  
     // Case 2.
     start = clock();
     for (int i=0; i&lt;10000000; i++)
        sqrt(pow(cos(1),2));
     ends = clock();
     cout &lt;&lt; (double) (ends - start) / CLOCKS_PER_SEC &lt;&lt; endl;
     return 0;
  }
  </code></pre>
  
  <h3>Results</h3>
  
  <ul>
  <li>Case 1 = 6.375</li>
  <li>Case 2 = 0.031</li>
  </ul>
  
  <p>Why is the first one is much slower, and if the second one is faster why dont we always write code that way? Anyway does the second pratice has a name?<br>
  I also wondered what happens if I create the variables outside the for loop in the first case, but the result was the same. Why?</p>