Professional Documents
Culture Documents
Syntax:
function [out1, out2, …,outN] = fun_name (in1,in2, …,inM)
coding
end
Above defines a function ‘fun_name’ that accepts inputs in1, in2, …,inM & returns
outputs out1, out2…,outN.
Function is called in the command window using the name of the function.
Valid function names begin with an alphabetic character, and can contain letters,
numbers, or underscores
INPUT AND OUTPUT PARAMETERS
The input & output variables can be scalars, vectors, matrices and strings.
MATLAB does not really distinguish between variables types until some
calculation or operation involving the variables is performed.
e.g. y=sin(x)
If ‘x’ is a scalar, ‘y’ is also a scalar.
If ‘x’ is a vector, ‘y’ is also a vector.
If ‘x’ is a matrix, ‘y’ is also a matrix.
FUNCTION EXAMPLE: ONE OUTPUT
Syntax:
function [out1, out2, …] = fun1 (in1,in2, …)
coding (fun2 used in coding)
end
function [out1, out2, …] = fun2 (in1,in2, …)
coding
end
Above defines a function ‘fun1’ that accepts inputs in1, in2, … & returns
outputs out1, out2,… and also using fun2.
The name of the variables in ‘fun2’ can be same as ‘fun1’ because ‘fun2’ will
work on a different space than ‘fun1’
(x(8),y(8))
(x(2),y(2))
Function with ‘plot’ command