The document discusses different types of functions in MATLAB:
1) Functions allow grouping code to perform tasks and operate in their own workspace separately from the base workspace. They can accept multiple inputs and outputs.
2) Anonymous functions can be defined inline without a file using the @ syntax.
3) Primary functions must be in a file but can call sub-functions defined there as well.
4) Nested functions are defined within another function and share its workspace. Private functions reside in a private subfolder and are only visible locally.
5) Global variables can be shared between functions by declaring them globally at the start of relevant files.
2. Anonymous Functions
Ananonymous functionis like aninline functionintraditionalprogramming languages, defined withina single
MATLAB statement. It consists of a single MATLAB expressionand any number of input and output arguments.
Youcandefine ananonymous functionright at the MATLAB command line or withina functionor script.
This way youcancreate simple functions without having to create a file for them.
The syntax for creating ananonymous functionfromanexpressionis
f = @(arglist)expression
Example
Inthis example, we willwrite ananonymous functionnamed power, whichwilltake two numbers as input and
returnfirst number raised to the power of the second number.
Create a script file and type the following code init:
power = @(x, n) x.^n;
result1 = power(7, 3)
result2 = power(49, 0.5)
result3 = power(10, -10)
result4 = power (4.5, 1.5)
Whenyourunthe file, it displays:
result1 =
343
result2 =
7
result3 =
1.0000e-10
result4 =
9.5459
Primary and Sub-Functions
Any functionother thanananonymous functionmust be defined withina file. Eachfunctionfile contains a required
primary functionthat appears first and any number of optionalsub-functions that comes after the primary function
and used by it.
Primary functions canbe called fromoutside of the file that defines them, either fromcommand line or fromother
functions, but sub-functions cannot be called fromcommand line or other functions, outside the functionfile.
Sub-functions are visible only to the primary functionand other sub-functions withinthe functionfile that defines
them.
Example
Let us write a functionnamed quadratic that would calculate the roots of a quadratic equation. The functionwould
take three inputs, the quadratic co-efficient, the linear co-efficient and the constant term. It would returnthe
roots.
The functionfile quadratic.mwillcontainthe primary functionquadratic and the sub-functiondisc, whichcalculates
the discriminant.
Create a functionfile quadratic.m and type the following code init:
function [x1,x2] = quadratic(a,b,c)
%this function returns the roots of
% a quadratic equation.
% It takes 3 input arguments
% which are the co-efficients of x2, x and the
%constant term
% It returns the roots
3. d = disc(a,b,c);
x1 = (-b + d) / (2*a);
x2 = (-b - d) / (2*a);
end % end of quadratic
function dis = disc(a,b,c)
%function calculates the discriminant
dis = sqrt(b^2 - 4*a*c);
end % end of sub-function
Youcancallthe above functionfromcommand prompt as:
quadratic(2,4,-4)
MATLAB willexecute the above statement and returnthe following result:
ans =
0.7321
Nested Functions
Youcandefine functions withinthe body of another function. These are called nested functions. A nested function
contains any or allof the components of any other function.
Nested functions are defined withinthe scope of another functionand they share access to the containing
function's workspace.
A nested functionfollows the following syntax:
function x = A(p1, p2)
...
B(p2)
function y = B(p3)
...
end
...
end
Example
Let us rewrite the functionquadratic, fromprevious example, however, this time the disc functionwillbe a nested
function.
Create a functionfile quadratic2.m and type the following code init:
function [x1,x2] = quadratic2(a,b,c)
function disc % nested function
d = sqrt(b^2 - 4*a*c);
end % end of function disc
disc;
x1 = (-b + d) / (2*a);
x2 = (-b - d) / (2*a);
end % end of function quadratic2
Youcancallthe above functionfromcommand prompt as:
quadratic2(2,4,-4)
MATLAB willexecute the above statement and returnthe following result:
ans =
0.7321
Private Functions
4. A private functionis a primary functionthat is visible only to a limited group of other functions. If youdo not want to
expose the implementationof a function(s), youcancreate themas private functions.
Private functions reside insubfolders withthe specialname private.
They are visible only to functions inthe parent folder.
Example
Let us rewrite the quadratic function. This time, however, the disc functioncalculating the discriminant, willbe a
private function.
Create a subfolder named private inworking directory. Store the following functionfile disc.m init:
function dis = disc(a,b,c)
%function calculates the discriminant
dis = sqrt(b^2 - 4*a*c);
end % end of sub-function
Create a functionquadratic3.minyour working directory and type the following code init:
function [x1,x2] = quadratic3(a,b,c)
%this function returns the roots of
% a quadratic equation.
% It takes 3 input arguments
% which are the co-efficients of x2, x and the
%constant term
% It returns the roots
d = disc(a,b,c);
x1 = (-b + d) / (2*a);
x2 = (-b - d) / (2*a);
end % end of quadratic3
Youcancallthe above functionfromcommand prompt as:
quadratic3(2,4,-4)
MATLAB willexecute the above statement and returnthe following result:
ans =
0.7321
Global Variables
Globalvariables canbe shared by more thanone function. For this, youneed to declare the variable as globalin
allthe functions.
If youwant to access that variable fromthe base workspace, thendeclare the variable at the command line.
The globaldeclarationmust occur before the variable is actually used ina function. It is a good practice to use
capitalletters for the names of globalvariables to distinguishthemfromother variables.
Example
Let us create a functionfile named average.mand type the following code init:
function avg = average(nums)
global TOTAL
avg = sum(nums)/TOTAL;
end
Create a script file and type the following code init:
global TOTAL;
5. TOTAL = 10;
n = [34, 45, 25, 45, 33, 19, 40, 34, 38, 42];
av = average(n)
Whenyourunthe file, it willdisplay the following result:
av =
35.5000