Skip to main content
Logo image

Section 8.5 Script Files Versus Function Files

  • Both script and function files are saved with the same extensions (.m).
  • The first executable line in a function file must be the function definition line. MATLAB kindly pre-populates the editor window with the expected syntax if you ask to create a new function file.
  • Variables in a function file are local to the function (their scope is limited to the function) while variables in a script file are recognized in the Command Window.
  • Script files can use variables defined in the workspace.
  • Script files are simply sequences of MATLAB commands (statements).
  • Function files can accept data through input arguments and can return data through arguments.
  • When a function file is saved, the name of the file should be the same as the name of the function.