file – Whats the difference between a script and a function in MATLAB?

file – Whats the difference between a script and a function in MATLAB?

This page says,

Scripts versus Functions

Scripts are m-files containing MATLAB
statements. MATLAB “functions are
another type of m-file. The biggest
difference between scripts and
functions is that functions have input
and output parameters. Script files
can only operate on the variables that
are hard-coded into their m-file. As
you can see, functions much more
flexible. They are therefore more
suitable for general purpose tasks
that will be applied to different

Scripts are useful for tasks
that dont change. They are also a way
to document a specific sequence of
actions, say a function call with
special parameter values, that may be
hard to remember.

There are more
subtle differences between scripts and
functions. A script can be thought of
as a keyboard macro: when you type the
name of the script, all of the
commands contained in it are executed
just as if you had typed these
commands into the command window.
Thus, all variables created in the
script are added to the workspace for
the current session. Furthermore, if
any of the variables in the script
file have the same name as the ones in
your current workspace, the values of
those variables in the workspace are
changed by the actions in the script.
This can be used to your advantage. It
can also cause unwanted side effects.

In contrast, function variables are
local to the function. (The exception
is that its possible to declare and
use global variables, but that
requires and explicit action by the
user.) The local scope of function
variables gives you greater security
and flexibility. The only way (besides
explicitly declared global variables)
to get information into and out of a
function is through through the
variables in the parameter lists.


One of the main differences between a script and a function is access to variables in the workspace. For example, suppose in the workspace, youve defined two variables a = 10 and b = 20. These variables are defined at the command line of the main prompt.

Script file – display_mult.m


Typing display_mult would display the product of a and b in the workspace, i.e. 10*20 or 200.

But if you defined a function, called display_mult defined in a file with the same name:

Function file – display_mult.m

function display_mult(a,b)

You would have to include the two variables as arguments to the function call. So, display_mult would not work this time since a and b does not exist in the functions workspace. You would have to include them by running display_mult(a,b) which will display the desired result.

Simple explanation

Each statement in a script is equivalent to typing them out at the command window of MATLAB. Youre just storing them before-hand in a file!

A function, on the other hand, takes in arguments and is a new workspace, separate from the main workspace.

Note: The end at the end of a function call is optional but I like to add it to make things organized. Of course, if you have multiple function definitions in a file they will all have to end with end. Also, you cannot have a script and a function definition in the same file.

file – Whats the difference between a script and a function in MATLAB?

Leave a Reply

Your email address will not be published. Required fields are marked *