Here's the recursive solution:
function power(x, n)
if n = 0 then
return 1
if n < 0 then
x := 1 / x
n := -n
if n is even then
half := power(x, n / 2)
return half * half
else
return x * power(x, n - 1)
The base case is , which returns . For negative , you flip to and make positive. Then you halve at each recursive step, giving time and space for the call stack.