5.7 Scripts

We have seen m-files which have the function declaration at the top. In practice these files create new MATLAB functions. In creating a function whcih we will call fun(a) we might use a variable like x. Now suppose that the variable x has a value in your session? What happens to the value x after you make a call to fun(a)? Nothing. The only way to change the value of x when running fun is to assign x=fun(a). The x inside the program fun.m behaves independently from the variable x in your session.

A script is an m-file without a function declaration at the top. A script treats variables differently than a function file. In a script, if x appears in a program, which you might call scrpt, and x has a value in your session, then a call to scrpt might change the value of x. If you do not make that function declaration, then the variables in your session can be altered. Sometimes this is useful but usually it is recommended that you use function files.

Next Section: 6. Keywords and Symbols


Created by F.G. Garvan (fgarvan@ufl.edu) on Thu, Oct 02, 97
Last update made Thu Oct 2 11:22:28 EDT 1997.
Back to 5.6 Misc.
Back to Table of Contents
EMAIL: fgarvan@ufl.edu fgarvan@ufl.edu