2 Questions about user defined function

7 次查看(过去 30 天)
hello
1.When using user_defined_function, do I have to put an 'end' at the end of code?
once it works without 'end' code, Is there any difference from using it?
function [a1,b1]=fn_name(y)
a1=(1/4).*(y.^3)
b1=5*5*y
2. why does not work if adding 'format compact'?
format compact
function [a1,b1]=fn_name(y)
a1=(1/4).*(y.^3)
b1=5*5*y
-> not working....
+)error code
Error: File: fn_name.m Line: 2 Column: 18
Local function name must be different from the script name.
thank you

采纳的回答

RAJA SEKHAR BATTU
RAJA SEKHAR BATTU 2022-10-27
Hi Kim,
If you are writing the function in a seperate file ex: fn_name, It doesn't matter if you write an end or not. Because the Matlab assumes you ended the function.
But In case, If you are writing a function inside a script. You need to make sure the function ended properly. Because Matlab will take it as a syntax error.
I hope you got this clear.
  4 个评论
RAJA SEKHAR BATTU
RAJA SEKHAR BATTU 2022-10-27
Yes, As It is not inside the function. Matlab takes it as a syntax error.
I hope everything is clear.
Please accept my answer If you got cleared.
RAJA SEKHAR BATTU
RAJA SEKHAR BATTU 2022-10-27
If you are writing function inside a script. Don't use the same function name as the script. Use anothor name, Matlab gets confuses with the local function name and script name.

请先登录,再进行评论。

更多回答(2 个)

John D'Errico
John D'Errico 2022-10-27
Why does it not work in the format compact question?
You had this:
format compact
function [a1,b1]=fn_name(y)
...
Simple rule: A function starts with the function header in the beginning of the file, typically it will be the first EXECUTABLE line. So you can have blank lines precede the function header. You can even have comments. But you cannot have executable commands. A format command is something you can use at the command line, or INSIDE the function. But a function is NOT a script. So had you put the format statement inside the function, thus after the function header, it would have worked.

Steven Lord
Steven Lord 2022-10-27
1.When using user_defined_function, do I have to put an 'end' at the end of code?
In a function file, either all of the functions in the file must end with an end or none of them can end with an end. Historically, prior to the introduction of nested functions (IIRC) functions didn't need to end with an end (and that behavior is still allowed) but if you only ended some of the functions in a file with an end figuring out where one ended and another began could get complicated. So it's all or none.
In a script file that contains a local function or in a class file, functions must end with an end.
2. why does not work if adding 'format compact'?
If you have a function inside a function file, the main function should have the same name as the file. If they differ, MATLAB will know the function by its file name (and will show a Code Analyzer warning for the function definition line.)
In a class file, if you have a function with the same name as the class it's the class's constructor. But classes don't need to define constructors if the "default" constructor is sufficient.
If you have a function inside a script file, which is the case for your example, then as stated in the first Note on this documentation page none of the functions in the script are allowed to have the same name as the script file. If you had a script named myscript.m and a function inside myscript.m named myscript, then this line of code inside the script would be ambiguous. Is it trying to run the script or call the local function with no input arguments?
myscript

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by