Contents Previous Next Subchapters

Defining Functions With Multiple Return Values
Syntax function [ variables ] = name ( arguments ) block
local function [ 
variables ] = name ( arguments ) block
variables ] = name ( arguments )
See Also defining functions , local functions , arg(0) , nargin , nargout

Description
Defines the function name, which executes the commands in block . The argument list for the function is specified by arguments. If the local keyword is present in the syntax, the function can only be referenced in the present file; i.e. it has file scope .

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.

Multiple Return Values
If you enter
     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

Determining Number Of Arguments And Return Values
The arg(0) notation can be used to determine the number of arguments and return values for this call to the function. If you enter
     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

Unspecified Return Values
If you assign a return value that is not requested by the call to the function, the operation will have no effect. In addition, if a return value is not assigned a value, it will have the type "novalue" by default. For example, if you enter
     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

Call By Value
Using the function definition syntax above, the value in the calling routine of the variables corresponding to arguments cannot be modified. This is often referred to as call by value. On the other hand, using the function definition syntax described in arguments , the value in the calling routine of the variables corresponding to its This is often referred to as call by address.

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.