title: Can a compiler optimize multiple same function calls
body: <p>There are tons of questions and good answers concerning compiler optimization about "redundant function calls" on SO (I won't post links), however, I could not find anything on <em>multiple same</em> function calls on SO.</p>
  
  <p>Say I have a code snippet like this:</p>
  
  <pre><code>void fairlyComplexFunction(const double &amp;angle)
  {
      //do stuff and call "sin(angle)" very often in here
  }
  </code></pre>
  
  <p>Calling <code>sin(angle)</code> is a <em>fairly expensive</em> operation and since <code>angle</code> is a const within the scope of <code>fairlyComplexFunction</code> every call of the sine will end up with the same result so only calling it once would be a better approach:</p>
  
  <pre><code>void fairlyComplexFunction(const double &amp;angle)
  {
      const double sineOfAngle = sin(angle);
      //do stuff and use sineOfAngle very often in here
  }
  </code></pre>
  
  <p>Is a compiler in any way able to detect things like these and optimize this for me or is the second example a better approach?</p>