Module 4: Modular Programming/Intro

I ntroducing modular programming. This is a programming design topic which promotes the

Goals of this module:

  1. Introduce concept of modular programming
  2. Build modular program using instructor provided sample
  3. Integrate student's own modules
  4. Validate Program
  5. Document Program

It is useful to encapsulate the solution of individual tasks into a single MATLAB m-file function. This general function can then be used in a variety of applications, with confidence, once the design, test, document steps are completed.

The two types of files most commonly used in MATLAB are script files and function files. Script files and function files are the same in that they both contain a sequence of MATLAB statements but are different in two important ways.

First, variables in MATLAB script files are global existing within the working environment whereas variables in MATLAB function files are local existing only within the environment of the function file itself. Secondly, the first line of MATLAB function files have a specific format that provides a mechanism for passing MATLAB variables between the global environment of a script file and the local environment of a function file. This allows function files to use script file variables and script files to use function file variables. Both script files and function files require names that end with a ".m" extension (e.g. name.m) and MATLAB distinguishes function files from script files by the specific format required by the first line of function files. Type "help script" or "help function" at the MATLAB command line prompt to learn how to use script and function files in MATLAB.

Page author: Dr. Kris Stewart
URL: http://www.stewart.cs.sdsu.edu/cs205/module4/Intro.html
Last updated: Feb. 18, 1998