Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://web.cecs.pdx.edu/~gerry/MATLAB/programming/scripts.html#scriptsVSfuncs" rel="noreferrer">This page</a> says,</p> <blockquote> <p><strong>Scripts versus Functions</strong></p> <p>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. </p> <p>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. </p> <p>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.</p> <p>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.</p> </blockquote> <p><strong>Example</strong></p> <p>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 <code>a = 10</code> and <code>b = 20</code>. These variables are defined at the command line of the main prompt.</p> <p><strong><em>Script file - display_mult.m</em></strong></p> <p><code>disp(a*b)</code>;</p> <p>Typing <code>display_mult</code> would display the product of <code>a</code> and <code>b</code> in the workspace, i.e. <code>10*20</code> or <code>200</code>.</p> <p>But if you defined a function, called display_mult defined in a file with the same name:</p> <p><strong><em>Function file - display_mult.m</em></strong></p> <pre><code>function display_mult(a,b) disp(a*b); end </code></pre> <p>You would have to include the two variables as arguments to the function call. So, <code>display_mult</code> would <strong>not</strong> work this time since <code>a</code> and <code>b</code> does not exist in the function's workspace. You would have to include them by running <code>display_mult(a,b)</code> which will display the desired result.</p> <p><strong>Simple explanation</strong></p> <p>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!</p> <p>A function, on the other hand, takes in arguments and is a "new" workspace, separate from the main workspace.</p> <p><strong>Note:</strong> The <code>end</code> 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 <code>end</code>. Also, you cannot have a script and a function definition in the same file.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload