Functions

advertisement
Functions
Scripts and Workspace
• scripts are just a collection of commands,
running a script is same as running the
statements in command window.
• Your scripts and command window shares
the same set of variables, also called the
workspace.
Passing a variable to a script
Command Window:
>>x=2;
>>script
2
>>x
x=5
script.m :
disp(num2str(x))
x = 5;
Global Variables
• Variables in scripts and command window
are shared, in other words, they are global.
• Global variables have been found to be
problematic, since your program could
erase a value needed by some other script,
or the values you depend on might be
changed by some other program. These
side-effects are difficult to find
• In large systems, name clashes would be
very common.
Passing Data to a Script
• Let’s say we have a script that sums the
values in a vector :
sum_vect.m :
sum = 0;
for x=1:length(v)
sum = sum + v(x)
end
Passing Data to a Script
• We have another program where we are
given three vectors: a,b, & c. We want to
take the average of all elements in them.
How can we make use of sum script?
v = a; sum_vect, suma = sum;
v = b; sum_vect, sumb = sum;
v = c; sum_vect, sumc = sum;
average = (suma+sumb+sumc)/(length(a) + length(b)+length(c))
A better way?
• Take the rem function
• Do we call it like:
>>numerator=7,denominator = 3; rem;
disp(num2str(result))
• We do:
>>disp(num2str(rem(7,3)));
• We just send our data as parameters, and the
expression will be evaluated to return value
of our function
Horizontal Line Example
• Rather than getting the input from user
every time, we can write our own function
horizontalLine:
function horizontalLine(a, b, y, g)
for x=a:b-1
putPixel(x, y, g);
end
>>>> horizontalLine(10, 300, 20, g)
Drawing a Rectangle
function drawRectangle(x1, y1, x2, y2, g)
x = y1;
while (x <= y2)
horizontalLine(x1, x2, x, g)
x = x + 1;
end
what about x? horizontalLine also have a variable x !
Functions have private workspaces
>> whos
Name
Size
fr
1x1
g
1x1
mines 400x400
Bytes Class
javax.swing.JFrame
sun.awt.windows.WGraphics
160000 logical array
Grand total is 160002 elements using 160000 bytes
>> horizontalLine(10, 200, 20, g)
>> x
??? Undefined function or variable 'x'.
Functions as Black Boxes
• Functions are machines that get some input
and produce some output.
• We cannot see, or shouldn’t care to see the
inner workings of a function, the local
variables used by the function and so on.
• functions cannot see or use variables that
exist in the context of the caller.
• when you pass input arguments to
functions, you are not passing variables, just
simple values.
function x
a = 10;
x1(a)
function x1(a1)
a1 =20;
x2(a1)
function x2(a1)
a1 =30;
x3(a1 + 10)
function x3(a3)
a1 =20;
a=10
a1=20
a1=30
a3=40
a1=20
Function calls
the only
information that
goes from a
function to
another is the
values of
parameters. The
callee cannot see
or know which
variables the
caller has
A closer look at putPixel function suite
• initWindow :
fr = javax.swing.JFrame('Drawing Pad');
fr.setSize(400, 400);
mines = rand(400, 400) > 0.99;
fr.show;
g = fr.getContentPane.getGraphics;
• it creates global variables mines and g
PutPixel function
function putPixel(x, y, g)
g.drawLine(x,y, x,y)
• Since putPixel cannot see the global
variable g, we have to pass it ourselves
• Note that we could also write:
function putPixel(x, y, graphicsObject)
• You can give any variable name you want
Functions that Return values
function minValue = minVector(v)
minValue = v(1);
for i = 2:length(v)
if (v(i) < minValue)
minValue = v(i);
end
end
>> v = [5 3 7 1 9 11]
v=
5
3
7
>> minVector(v)
ans =
1
1
9
11
Function that Returns multiple values
function [minValue, minIndex] = minVector(v)
minValue = v(1);
minIndex = 1;
for i = 2:length(v)
if (v(i) < minValue)
minValue = v(i);
minIndex = i;
end
end
>> minVector(v)
ans =
1
>> [a b] = minVector(v)
a=
b=
1
4
Complete Form
function [minValue, minIndex] = minVector(v)
%MINVECTOR function that returns minimum of a vector
% it returns two values, one the minimum
% value in the vector, the other, the index of the minimum value
minValue = v(1);
minIndex = 1;
for i = 2:length(v)
if (v(i) < minValue)
minValue = v(i);
minIndex = i;
end
end
help and lookfor
>> lookfor minVector
MINVECTOR function that returns minimum of a vector
>>
>> help minVector
MINVECTOR function that returns minimum of a vector
it returns two values, one the minimum
value in the vector, the other, the index of the minimum
value
Download