General Format of a Function Definition
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 ;
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- functions united states naval academy
- web interface for matlab er publications
- usb interface design scaglione lab
- c s 160 introduction to programming with application to
- building finite element analysis programs as distributed
- introduction to matlab
- general format of a function definition
- using matlab in a multivariable calculus course
Related searches
- format of a scientific paper
- basic format of a white paper
- format of a research paper
- format of a notice writing
- format of a personal statement
- format of a report
- find inverse of a function calculator
- derivative of a function at a point
- period of a function sin
- period of a function equation
- period of a function calc
- differential of a function calculator