MATLAB Answers

0

Use of "return" in simple if statement

Asked by MiauMiau on 25 Jan 2013
Latest activity Commented on by Walter Roberson
on 7 Mar 2017
Hi
I have of course read the explanations regarding the return statement. Still in the following case:
if (parameter < 1)
disp('Wrong parameters...');
return
end
I don't see what difference it makes if we use "return" here or we dispense with it..
Thanks

  1 Comment

Just a suggestion - instead of disp() you might use
uiwait(warnglg('Wrong parameters...'));
I think it's a little nicer to have a popup message box. Better yet, use sprintf() to create your string and tell your users what they entered and what the valid range is.

Sign in to comment.

Tags

2 Answers

Answer by Walter Roberson
on 25 Jan 2013
 Accepted Answer

If you do not "return" then the rest of the function would be executed.

  3 Comments

Maybe this is off topic but I could not find the solution anywhere else. What is the equivalent command in R for "return"? I am new to programming so please do not mind if this is a very basic question. Thanks in advance! :)
You'll be better off asking a forum dedicated to R!
return(g)
This tells me that R has return() and that typically you would need to tell it what to return.

Sign in to comment.


Answer by Evgeny Pr on 25 Jan 2013
Edited by Evgeny Pr on 25 Jan 2013

You need to use ERROR function, do not return:
function myfunc(parameter)
if (parameter < 1)
error('Wrong parameters...');
end
% continue
end
...or:
function myfunc(parameter)
if (parameter < 1)
disp('Wrong parameters...');
else
% continue
end
end

  1 Comment

Sign in to comment.