Documentation Center

  • Trial Software
  • Product Updates

Scripts vs. Functions


Program files can be scripts that simply execute a series of MATLAB® statements, or they can be functions that also accept input arguments and produce output. Both scripts and functions contain MATLAB code, and both are stored in text files with a .m extension. However, functions are more flexible and more easily extensible.

For example, create a script in a file named triarea.m that computes the area of a triangle:

b = 5;
h = 3;
a = 0.5*(b.* h)


a =



function a = triarea(b,h)
a = 0.5*(b.* h);


a1 = triarea(1,5)
a2 = triarea(2,10)
a3 = triarea(3,6)
a1 =
a2 =
a3 =


Related Examples

More About

Was this topic helpful?