I am trying to write a recursive code to check whether a string is palindrome or not, when i am running a trial case a error message shows up stating "he function call palindrome('madam') caused an error and did not complete (MATLAB:To​oManyOutpu​ts)".

4 次查看(过去 30 天)
% I am writing a code to find the palindrome of a string using recursion but i am getting the error The function call palindrome('madam') caused an error and did not complete (MATLAB:TooManyOutputs)
function palindrome(v)
if length(v) <= 1
true
return;
end
if v(1) ~= v(end)
false
return;
end
palindrome(v(2:end-1));
end
  2 个评论
John D'Errico
John D'Errico 2020-10-20
编辑:John D'Errico 2020-10-20
Does your function actually return ANY output argument?
What happens when you just type false or true on the command line? Does that return anything? Or does it just dump something to the command window?
I think you need to do some reading about functions and how to use them.
When you write a function header, one thing you need to do is tell MATLAB what variables to return.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2020-10-20
If you want to return an output argument then it must be declared in the function, e.g.:
function out = palindrome(v)
if length(v) <= 1
out = true;
return
end
if v(1) ~= v(end)
out = false;
return
end
out = palindrome(v(2:end-1));
end
You still have a few more bugs to fix...

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by