2.10 Functions (Macros)

Functions are collections of commands which are executed in a new environment thus isolating function variables from the original environments variables. Functions can be created and executed in a number of different ways. Furthermore, functions can pass arguments, have programming features such as conditionals and loops, and can be recursively called. Functions can be arguments to other functions and can be elements in lists. The most useful way of creating functions is by using a text editor, however, functions can be created directly in the Scilab environment using the syntax function or the deff primitive.

--> function [x]=foo(y)
-->    if y>0 then, x=1; else, x=-1; end
--> endfunction
 
--> deff('[x]=foo(y)','if y>0 then, x=1; else, x=-1; end')
 
--> foo(5)
 ans       =
 
    1.  
 
--> foo(-3)
 ans       =
 
  - 1.
Usually functions are defined in a file using an editor and loaded into Scilab with:
exec('filename').
This can be done also by clicking in the File operation button. This latter syntax loads the function(s) in filename and compiles them. The first line of filename must be as follows:
function [y1,...,yn]=macname(x1,...,xk)
where the yi's are output variables and the xi's the input variables.

For more on the use and creation of functions see Section 3.2.