0% found this document useful (0 votes)
28 views

Todo User-Defined Functions - Bad Examples

The document describes the general format for defining functions in MATLAB. It states that all MATLAB functions must be defined in their own M-file with the same name as the function. The format includes specifying a return value, function name, and any input parameters in the header. The body of the function then performs calculations to assign a value to the return value, usually on the last line. Several examples of incorrect function definitions are provided and compared to a correct format.

Uploaded by

anuj tatti
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Todo User-Defined Functions - Bad Examples

The document describes the general format for defining functions in MATLAB. It states that all MATLAB functions must be defined in their own M-file with the same name as the function. The format includes specifying a return value, function name, and any input parameters in the header. The body of the function then performs calculations to assign a value to the return value, usually on the last line. Several examples of incorrect function definitions are provided and compared to a correct format.

Uploaded by

anuj tatti
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

General Format of a Function Definition

All functions in Matlab have the following general format:

function return_value = function_name( parameters )


% comments describing the function

% body of the function, including code to compute the value of


% return_value, i.e., somewhere in the body (usually the last
% line) will be a line of code that starts out:
%
% return_value = ...

where

 function_name is the name of the function (which must also be the name of the
m-file it is in)
 return_value is the name of the output
 parameters are the names of the inputs, separated by commas

Each function must be in its own m-file and the name of that m-file must be the same as
the name of the function.  Note that the name of the return value (output) and the names
of the parameters are the programmer's choice (and can be the same as names used in
other functions or scripts).

All functions in Matlab are defined this way, including the ones provided by Matlab. 
This means that somewhere among the files that Matlab uses as it runs are cos.m, sin.m,
and exp.m (among many others) which contain the definitions for the cosine, sine, and
exponential functions, respectively.

The following examples show several incorrect functions followed by a correct version of
the function. Each function is supposed to cube the input (assuming the input is a scalar
value).

Bad Example 1:

function c = cube(x)
y = x^3 ; % The value of the output c is not defined.

Bad Example 2:

c = cube(x) % the word ‘function’ is missing.


c = x^3 ;

Bad Example 3:

function cube(x) % The output is not specified.


c = x^3 ;
Correct Example:

function c = cube(x)
c = x^3 ;

You might also like