Use of "return" in simple if statement

159 ビュー (過去 30 日間)
MiauMiau
MiauMiau 2013 年 1 月 25 日
コメント済み: Walter Roberson 2017 年 3 月 7 日
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 件のコメント
Image Analyst
Image Analyst 2013 年 1 月 25 日
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.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2013 年 1 月 25 日
If you do not "return" then the rest of the function would be executed.
  3 件のコメント
Guillaume
Guillaume 2017 年 3 月 7 日
You'll be better off asking a forum dedicated to R!
Walter Roberson
Walter Roberson 2017 年 3 月 7 日
return(g)
This tells me that R has return() and that typically you would need to tell it what to return.

サインインしてコメントする。

その他の回答 (1 件)

Evgeny Pr
Evgeny Pr 2013 年 1 月 25 日
編集済み: Evgeny Pr 2013 年 1 月 25 日
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 件のコメント
MiauMiau
MiauMiau 2013 年 1 月 25 日
thanks

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by