tags:

views:

114

answers:

1

what are the differences between a MATLAB script file and a MATLAB function?

+7  A: 

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 data.

Scripts are useful for tasks that don't 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 it's 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.

Example

One of the main differences between a script and a function is access to variables in the workspace. For example, suppose in the workspace, you've 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*

disp(a*b);

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)
   disp(a*b);
end

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 function's 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. You're 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.

Jacob
example?can u give me some example?
izzat
Added an example and expanded on the solution
Jacob

related questions