User-defined functions are similar to the MATLAB pre-defined functions. A function is a MATLAB program that can accept inputs and produce outputs. A function can be called or executed by another program or function.
Code for a function is done in an Editor window or any text editor same way as script and saved as m-file. The m-file must have the same name as the function.
2. Introduction
O User-defined functions are similar to the
MATLAB pre-defined functions
O A function is a MATLAB program that can accept
inputs and produce outputs.
O A function can be called or executed by another
program or function.
O Code for a function is done in an Editor window
or any text editor same way as script and saved
as m-file.
O The m-file must have the same name as the
function.
2
3. Introduction…
OThe convention for naming functions is the
same as for variables.
OIt is important that you give meaningful
variable names to variables inside a function
that you write, so that you and others can
understand what the function does.
OThe function M-file must be saved in your
current directory (otherwise it must be in the
path)
3
4. Why use functions?
OFunctions provide reusable code
OUse same code in more than one place in
program without rewriting code
OReuse code by calling in different programs
OMake debugging easier
4
5. Before you start …
OIdentify the function
ODecide the function name
ODecide the input variables
ODecide the output variables
OFile name must be the function name.
5
6. Function Syntax
function[a, b, c]= basicmath(x,y)
%BASICMATH Basic Mathematical function
% basicmath(x,y) is a sample matlabfunction to perform
% basic mathematical operations on input variables x & y
% outputs of the function are sum, difference and product
% of input arguments.
a = x + y;
b = x – y;
c = x * y;
6
Output arguments
Executable code
Comments about
the function
Name of the function
Input arguments
declaration
statement
7. Syntax …
O The declaration statement function is used to define the
file as a function.
O It must be typed in lower case letters.
O Input arguments
O Typed in side the parentheses ( )
O Used to transfer data into function from calling program.
O Can be zero or more input arguments.
O Output arguments
O Types inside the square brackets [ ]
O Used to transfer data out of function to calling program.
O Can be zero or more output arguments.
O Give a meaningful variable name.
O Rules for giving function name is same as the rules for
variable names.
7
8. More on Function Arguments
O Functions have private workspaces
O Variables that are created inside of a user-defined
function are referred to as local variables.
O They can only be accessed from inside of that
function.
O After the function completes its operations, the
local variables are deleted from memory.
O The only variable that appears in the workspace
is the output of the function.
O Conversely, functions cannot access variables
from the workspace. (with the exception of any input
parameters they might have or “global variables”---see MATLAB
help on this matter).