|
Contents | Previous | Next | Subchapters |
| Syntax |
function [ variables ] = name ( arguments ) blockvariables ] = name ( arguments ) blockvariables ] = name ( arguments )
|
| See Also | defining functions , local functions , arg(0) , nargin , nargout |
Using the function definition syntax above,
the return values of the function are specified by variables.
Using the function definition syntax described in
Defining Functions a single return value
is specified in the return
statement.
clear
function [x, y] = f(a, b) begin
x = a^2
y = b^3
end
[u, v] = f(2, 3)
print u, v
O-Matrix will respond
4 27
clear
function [x, y] = f(a, b) begin
print arg(0)
x = a
end
[u, v] = f(5)
O-Matrix will respond
[ 1 , 2 ]
because there is one arguments and two return values in the call
to the function f.
If you continue by entering
[u] = f(5, 6)
O-Matrix will respond
[ 2 , 1 ]
because there are two arguments and one return value in the call
to the function f.
If you continue by entering
u = f(5, 6)
O-Matrix will respond
2
which is just the number of arguments
because the number of return values is not specified in the
call to the function.
In this case only the first return value is used.
You can see this by entering
print u
to which O-Matrix will respond
5
clear
function [x, y, z] = f(a, b, c) begin
x = a
z = c
end
[u, v] = f(5, 6, 7)
print u, v
O-Matrix will respond
5 novalue
If you enter
clear
function f(x) begin
x = 5
return x
end
function [y] = g(x) begin
x = 5
y = x
end
x = 4
print f(x)
O-Matrix will respond
5
If you continue by entering
print x
O-Matrix will respond
5
If you then enter
x = 4
print g(x)
O-Matrix will respond
5
if you continue by entering
print x
O-Matrix will respond
4
because only the local value of x
with in the function g(x) was modified.