Why all the code in a MATLAB program are just functions

1 view (last 30 days)
Josep Llobet
Josep Llobet on 6 Jul 2021
Commented: Josep Llobet on 6 Jul 2021
Hello,
I have a downloaded code which is all functions, and when I run it it (F5) it works.
The code is a GUI which execute image analysis functions.
When I contract all the function drop-downs (with Ctrl + "," ), all the code is based in funtions, there is no one no-function code, and it run correctly anyway when I execute it.
I don't understand that all the code runs without any no-function code, which in my understanding it would be the necessary for to run the functions and the code.
Thank you very much.
Josep
  2 Comments
Josep Llobet
Josep Llobet on 6 Jul 2021
I did the MATLAB Onramp, and some MATLAB Fundamentals, from MATLAB Academy.
I have tryied to comprobe the execution of the first funcion running the probafunction.m next code:
function probafunction
disp("hello")
end
function probafunction2
disp("Hello2")
end
Which displays:
>> probafunction
hello
Is just run the first function.
Modifying the first function inside:
function probafunction
disp("hello")
%Modifying that↓
probafunction2
end
function probafunction2
disp("Hello2")
end
The command Window displays the next:
>> probafunction
hello
Hello2
I came from Python, where if I am not wrong, the function does not execute unless it is defined in the script.
Thank you very much for the comment!

Sign in to comment.

Accepted Answer

Jan
Jan on 6 Jul 2021
"No function code" is code written to a "script". Scripts share their workspace (the list of loclly used variables) with their caller. This has some sever disadvantages and professional code avoid scripts, but use functions with exactly defined inputs and outputs.
If a script does not use variables defined or used anywhere else, it can be converted to a function easily:
% This is my script file called "hello.m":
disp('hello')
To:
function hello
% This is my hello function stored in the file "hello.m":
disp('hello')
end
Both codes are equivalent and if you start them by F5 (green triangle) both do exactly the same.
"without any no-function code" is the standard case. Only tiny hacks or experimental code of beginners can be stored in scripts (an some very specific exceptions, where scripts are really the best choice).
  1 Comment
Josep Llobet
Josep Llobet on 6 Jul 2021
Thank you very much Jan for the comments and advices about using functions and not scripts in the code, and for the code which you exemplifyies.
I didn't know that functions are executed without being defined in the script. I came from Python and R studio, where if I am not wrong functions needs to be defined in the scripts.
I didn't know about the fact that @Rik comment about is just the first function which run in the running of the script.
Thank you again

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by