Suppose I have a function double F(double x) and let's assume for the sake of this example that calls to F are costly.
Suppose I write a function f that calculates the square root of F:
double f(double x){
   return sqrt(F(x));
}
and in a third function sum I calculate the sum of f and F:
double sum(double x){
   return F(x) + f(x);
}
Since I want to minimise calls to F the above code is inefficient compared to e.g.
double sum_2(double x){
   double y = F(x);
   return y + sqrt(y);
}
But since I am lazy, or stupid, or want to make my code as clear as possible, I opted for the first definition instead.
Would a C/C++ compiler optimise my code anyway by realizing that the value of F(x) can be reused to calculate f(x), as it is done in sum_2?
Many thanks.
 
     
     
    