This function is inadequate for several reasons:
It's buggy. Notice that
value
is declared as anint
rather than adouble
, which means that if you try to computepower(1.5, 1)
, you'll get back 1 rather than1.5
. In fact, it will be wrong on almost all inputs.It doesn't handle negative exponents. Try computing
power(2, -1)
. The correct answer is0.5
, but your function (after fixing theint
bug noted above) will return 1 rather than0.5
. You can fix this pretty easily (you could, for example, computepower(2, 1)
and then take the reciprocal), but it's troublesome as currently written.It's slow. Most exponentiation, when the power is an integer, is computed using an algorithm called exponentiation by squaring, which is considerably faster than your code. Exponentiation by squaring will do Θ(log Y) multiplications, compared to the Θ(Y) multiplications your code makes. It will take exponentially longer for your function to complete.
It doesn't handle fractional exponents. Try computing
power(1.5, 1.5)
. You'll get the wrong answer because the exponent is anint
, not adouble
. Correcting this isn't easy; search around on Stack Overflow for other questions on how to implement this properly.It reinvents the wheel. At a fundamental level, you should ask yourself why you're rewriting a function provided to you by the language's math libraries. This can introduce bugs or inefficiencies into the program (see the earlier bullet points) and at the end of the day you haven't increased the functionality.
Hope this helps!