MATLAB Answers

"Local function name must be different from the script name" error

376 views (last 30 days)
Hi, I wrote a simple code but I can't get output of the function because of this error. Name of the file is "main.m".
And it also says that this is a local function. Should I/how can I make it main function? The code is:
clear;
function [any, tri, sqr, rect] = main(number_of_sticks)
%This function will be used for finding if we can construct
%equilateral triangle/square/rectangle
tri=(rem(number_of_sticks,3)==0);
sqr=(rem(number_of_sticks,4)==0);
rect=(rem(number_of_sticks,2)==0);
any=(tri==1 || sqr==1 || rect==1);
end

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 2 Mar 2021
Edited: Stephen Cobeldick on 2 Mar 2021
clear; % !!!!!!!!!! Delete this line !!!!!!!!!
function [any, tri, sqr, rect] = main(number_of_sticks) % first line of the file
Putting clear on the first line makes your file a script and causes that error (because you named the script file with the same name as the local function). Putting clear serves no purpose. Do not add things that serve no purpose.
I strongly recommend that you avoid using the variable name any.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by