Previous Up Next

13  Local and Global variables

If a variable in a function is not defined (i.e. is not in the input parameters), then it is automatically considered as a global variable. The current value of this variable is used in the function. Functions can be invoked with less input or output parameters. Here is an example:
function [y1,y2]=f(x1,x2)
y1=x1+x2
y2=x1-x2

-->[y1,y2]=f(1,1)
 y2  =
    0.  
 y1  =
    2.  

-->f(1,1)
 ans  =
    2.

-->f(1)
y1=x1+x2; y2=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. 

Previous Up Next