Pow function and exponent

Does the pow(base,exponent) function check whether the exponent is 0 (thus returning 1) before computing, or does it compute the base first? The reason I ask is because I'm using a function as base, and wanting to save time I'd like the program not to run the function if the exponent is 0.
Last edited on
It doesn't compute the base at all. The base is passed to it via a function parameter.

If you are passing a function, your function will be run before pow is ever called.

For example.. this:

 
pow( func(), exp );


In this code, it is impossible for pow() to run before func(). func() will execute first, and whatever it returns when get passed to pow.

If you want to shortcut that and only compute the base if the exp is nonzero.. then you can do this:

1
2
3
4
5
double answer;
if(exp != 0)
  answer = pow( func(), exp );
else
  answer = 1;
It is noteworthy that if you are passing functions as parameters, you CANNOT assume that they will be called left to right or right to left. This happened to me where my functions were designed to be called in a certain order, left to right, but instead they were being called right to left.
Ok, thanks for answers! I will change my code to something like Disch's suggestion.
Topic archived. No new replies allowed.