Skip to main content
Logo image

Section 8.2 Introduction to Function Files

Function files are created and edited, like script files, in the Editor/Debugger Window that MATLAB offers (although you could also write functions in any other editor if you wished). They are also saved with the extension .m, just like script files.
You can use your own user-defined functions just like any other MATLAB-defined functions by calling them with their name in any desired expression.
If you want to use your own functions in your code you need to make sure the function files are either located in your working directory or else in a directory that is included in your search path.
Mathematical expressions must be written according to the dimensions of the arguments (scalars, vectors, arrays): you need to decide whether to use linear algebra rules or element-by-element calculations.
Let’s take a look at an example of a function file in MATLAB.
File: SphereVolArea.m
function[V,A] = SphereVolArea(radius)    
% SphereVolArea returns the volume and area of a sphere of radius r
%
% Inputs:
%  radius    - radius of sphere
%
% Outputs:
%  V         - volume of sphere
%  A         - area of sphere
%

r2 = radius*radius;  % radius squared, note the semicolon to prevent echoing
r3 = r2*radius;      % radius cubed

V = 4/3 * pi * r3;   % assignment of output arguments
A = 4 * pi * r2;
Let’s look at the various components of the function file:
  • Function definition line: first line of the function (starts with "function [V,A] ..." in this example).
  • H1 line: first line of comments (starts with "SphereVolArea returns..." in this example)
  • Help text: the remaining comments at the beginning of the function
  • Function body: all remaining lines of the function
  • Assignment of output arguments: the lines of code where the outputted variables are assigned (the last two lines in this example, since V and A are the output variables in the function definition line)
The function definition line specifies the name of the function (which has to agree with the file name), the vector of return parameter(s), and the function’s argument(s).
The H1 line is a short description of your function which contains key information that can be searched for using the MATLAB lookfor function.
lookfor some_word searches for some_word in all the H1 lines of the functions in your path.
Here is an example. Suppose the above SphereVolArea function file is in your working directory (or your search path). Then:
>> lookfor volume
SphereVolArea                  - SphereVolArea returns the volume and area of a sphere of radius r
xyzvchk                        - Check arguments to 3-D volume data routines.
volume                         - Volume of the alpha shape
volvec                         - Volume Visualization
reducevolume                   - Reduce volume dataset.
slice                          - Volumetric slice plot.
subvolume                      - Extract subset of volume dataset.
volumebounds                   - Returns x,y,z and color limits for volume data.
To specify that you with the lookfor function to search all text lines and not just the H1 line, use the optional parameter -all: lookfor -all some_word
The help text of a function is displayed when the user types: help function_name.
>> help SphereVolArea
SphereVolArea returns the volume and area of a sphere of radius r.

Inputs:
 radius   - radius of sphere

Outputs:
 V        - volume of sphere
 A        - area of sphere
>> help sqrt
sqrt   Square root.
   sqrt(X) is the square root of the elements of X. Complex results are produced if X is not positive.

   See also sqrtm, realsqrt, hypot.

   Documentation for sqrt
   Other functions named sqrt
If you’d like to know some related functions you could use the lookfor function to find them:
>> lookfor sqrt
sqrtm                          - Matrix square root.
sqrtm_tbt                      - Square root of 2x2 matrix from block diagonal of Schur form.
sqrtm_tri                      - Square root of quasi-upper triangular matrix.
realsqrt                       - Real square root.
sqrt                           - Square root.
...