Output argument 'answers' (and maybe others) not assigned during call to "..."
Show older comments
I am trying to create a function that sums up all the values of the fibonacci series using only recursion, but I keep getting this error message...
Output argument "answer" (and maybe others) not
assigned during call to "sum_of_fibo>fibo".
Error in sum_of_fibo (line 2)
summa = sum(fibo(n:-1:0));
Here's my code...can someone help?
function summa = sum_of_fibo(n)
summa = sum(fibo(n:-1:0));
end
function answer = fibo(n)
if n == 0
answer = 1;
elseif n == 1
answer = 1;
elseif n > 1
answer = fibo(n-1) + fibo(n-2);
end
end
Accepted Answer
More Answers (1)
Any time you write if... elseif... statements you should always include a final else statement, even if you think you've covered every possibility. The final else statement can either define a default value for answer or in can throw an error.
Example 1
function answer = fibo(n)
if n == 0
answer = 1;
elseif n == 1
answer = 1;
elseif n > 1
answer = fibo(n-1) + fibo(n-2);
else
answer = -1; % Default value
end
Example 2
function answer = fibo(n)
if n == 0
answer = 1;
elseif n == 1
answer = 1;
elseif n > 1
answer = fibo(n-1) + fibo(n-2);
else
error('''n'' must be greater or equal to 0.') % throw error
end
9 Comments
James Metz
on 15 Apr 2020
But 0 is neither positive nor negative and many users would interpret that error message as "any value >0".
Also, your 2nd condition allows for any positive number including 0.1111111111. If you want to limit the user to integers greater or equal to 0, you could do two things.
1) at the beginning of the function,
assert(isscalar(n) & mod(n,1)==0 & n>=0, '''n'' must be an integer greater or equal to 0.')
2) In this case, the else statement would never be reaced but just for safe measures, you could copy the error message there, too.
else
error('''n'' must be an integer greater or equal to 0.') % throw error
James Metz
on 15 Apr 2020
First, the fprintf() statement does not solve the problem and it will actually throw an error because answer is still not defined. Take a moment to think about the response from Image Analyst and myself and why the fprintf solution doesn't fix the problem. The statement in your fprintf is also misleading since it tells users that they can enter non-integer values (unless you want that).
Second, if n can be a vector, all of the function needs changed. Conditional statements are not designed to test elements of an array. Instead, you want to use indexing which is the main power of Matlab.
All of your conditions can be replaced by:
answer = nan(size(n));
answer(n==0 | n==1) = 1;
answer(n > 1) = fibo(n(n > 1)-1) + fibo(n(n > 1)-2);
assuming the fibo function works properly.
Update: I just saw that the fibo() function is recursive so this won't due.
James Metz
on 15 Apr 2020
Here's a hint for a simple solution that allows for vector input. This all goes in 1 m-file.
function answer = fibo(n)
answer = nan(size(n));
for i = 1:numel(n)
answer(i) = fibfcn( . . .);
end
function out = fibfcn(n)
% insert code
James Metz
on 15 Apr 2020
Adam Danz
on 17 Apr 2020
Out of memory. The likely cause is an infinite recursion within the program.
Error in sum_of_fibo (line 5)
summa = fibo(n) + jff(n-1);
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!