MATLAB Answers

putting return to a function

37 ビュー (過去 30 日間)
Salvatore Mazzarino
Salvatore Mazzarino 2012 年 10 月 13 日
I have created a function and into it I call another function.
function my_function()
value = another_function(foo);
end
my doubt is: inside another_function I have to put return or I may not include return keyword?

  0 件のコメント

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

回答 (3 件)

Star Strider
Star Strider 2012 年 10 月 13 日
The documentation for Nested Functions says:
Typically, functions do not require an end statement. However, to nest any function in a program file, all functions in that file must use an end statement.
So another_function has to have an end statement.

  3 件のコメント

Salvatore Mazzarino
Salvatore Mazzarino 2012 年 10 月 13 日
I asked about return keyword and not end keyword
Star Strider
Star Strider 2012 年 10 月 13 日
Since I could not find a discussion in the documentation that specifically answered your question, I experimented and changed an ODE function that contains nested functions so that one of the nested functions was closed by return (instead of end as I wrote it originally) and got this error:
The function "Rigid" was closed with an 'end', but at least one other function definition was not. To avoid confusion when using nested functions, it is illegal to use both conventions in the same file."
After some additional experimentation, return will not work even if all nested functions and the parent function are closed by return. So return will not work in the place of end for a nested function or the parent function. It is necessary to use end in that situation.
You can use return as usual in other places in nested and parent functions, but you cannot close nested or parent functions with return.
NOTE: I am using 2012b.
Image Analyst
Image Analyst 2012 年 10 月 13 日
If they're not nested, but merely contained within the same m-file, it will work with no ends:
% m-file test.m with no ends anywhere
function test()
% Call sub1.
foo = 30;
value = sub1(foo)
% Conclude function test with a "return", not an "end"
return;
function value = sub1(foo)
[X,Y,Z] = peaks(foo);
surfc(X,Y,Z)
colormap hsv
axis([-3 3 -3 3 -10 5])
value = X(1,:); % Return something.
% Conclude function sub1 with a "return", not an "end"
return;
Perhaps this will meet his needs. From his limited description, it doesn't appear that nesting is really needed and I'm not sure he even knows the difference. Of course, sub1 could also exist in another file, sub1.m, and that would work too.

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


Image Analyst
Image Analyst 2012 年 10 月 13 日
You can put returns wherever necessary in the function. At the end of the function it is optional (not necessary) but you can if you want

  0 件のコメント

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


Matt Fig
Matt Fig 2012 年 10 月 13 日
編集済み: Matt Fig 2012 年 10 月 13 日
Salvatore, are you asking how to make the function return the value that will be assigned to 'value' inside my_function? In MATLAB, unlike some other languages, there is no need to tell the function which value to return to the caller by a special call to the return keyword. For example, put this in your local directory:
function out = mysquarefcn(in)
out = in.*in;
Once you save that as mysquarefcn.m, the from the command line call it like this:
>> mysquarefcn(3)
>> G = mysquarefcn(3)

  0 件のコメント

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

Community Treasure Hunt

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

Start Hunting!

Translated by