function [y1,y2]=f(x1,x2) y1=x1+x2 y2=x1-x2 endfunction
-->[y1,y2]=f(1,1) y2 = 0. y1 = 2. -->f(1,1) ans = 2. -->f(1) y1=x1+x2; !--error 4 undefined variable : x2 at line 2 of function f -->x2=1; -->[y1,y2]=f(1) y2 = 0. y1 = 2. -->f(1) ans = 2.
Note that it is not possible to call a function if one of the parameter of the calling sequence is not defined:
function [y]=f(x1,x2) if x1<0 then y=x1, else y=x2;end endfunction
-->f(-1) ans = - 1. -->f(-1,x2) !--error 4 undefined variable : x2 -->f(1) undefined variable : x2 at line 2 of function f called by : f(1) -->x2=3;f(1) -->f(1) ans = 3
Global variable are defined by the global
command. They can
be read and modified inside functions. Enter help global for
details.