A recursive definition of an where a is an integer and n is a non-negative integer
follows:
a^nif=1 if n = 0
a^n=a*a^(n-1) if n>0
Write a recursive function called mypower, which receives a and n and returns the
value of an by implementing the previous definition. Note that the program should
not use ^ operator anywhere; this is to be done recursively instead! Test the
function.
I tried this one but cant work
function power=mypower(a,n)
if n==0
power(a,n)=1;
else
power(a,n)=a*mypower(a,n-1);
Thanks
end
littleO described the problem in the comments. The
powervariable should be a scalar, but you are treating it as an array. By indexing usinga, you will get an error if this is not an integer.The code should be as follows: