Matlab error: input argument not defined -
i wrote function as:
function f = factorial(x) f = prod(1:x); f =factorial(5); end
but when tried running it, says input argument not defined. what's wrong this?
you have defined function recurses endlessly.
f = factorial(5);
in third line call function again, again call function once reaches third line, again call function... never done.
when implementing recursive solution need provide base case. here's example calculating factorials.
function f = factorial(x) if x == 0 % base case f = 1; else f = x*factorial(x-1); % recursive case end end
example:
>> factorial(5) ans = 120
as your
input argument not defined
problem, need tell used input argument. above example, any* integer x>=0
should work.
*as long f
has enough bytes hold result.
Comments
Post a Comment