Next: 3.2.4 Special Function Commands
Up: 3.2 Defining and Using
Previous: 3.2.2 Loading Functions
If a variable in a function is not defined (and is not among the input
parameters) then it takes the value of a variable having the same name in the
calling environment. This variable however remains local in the sense that
modifying it within the function does not alter the variable in the calling
environment unless resume is used (see below). 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;
!--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
-->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
Next: 3.2.4 Special Function Commands
Up: 3.2 Defining and Using
Previous: 3.2.2 Loading Functions
Scilab Group