functions

Scilab Data type
Last update : 08/02/2005

functions - Scilab procedures and Scilab objects

### Description

Functions are Scilab procedures ("macro", "function" and "procedure" have the save meaning).

### Function definition

Usually, they are defined in files with an editor and loaded into Scilab by `getf` or through a library (see `lib` or `genlib`). But They can also be defined on-line (see `deff` or `function` o. A function is defined by two components:

  - -a "syntax definition" part as follows:

         function [y1,...,yn]=foo(x1,...,xm)
         function [y1,...,yn,varargout]=foo(x1,...,xm,varargin)

  - -a sequence of scilab instructions.

The "syntax definition" line gives the "full" calling syntax of this function. The `yi` are output variables calculated as functions of input variables `xi` and variables existing in Scilab when the function is executed.

### Calling function

Usually function calling syntax is `[y1,...,yn]=foo(x1,...,xm)`. Shorter input or output argument list than definition ones may be used. In such cases, only the first variables from the left are used of set. The `argn` function may be used to get the actual number of calling arguments.

It is also possible to use "named argument" to specify input arguments: suppose function `fun1` defined as `function y1=fun1(x1,x2,x3)` then it call be called with a syntax like `y=fun1(x1=33,x3=[1 2 3])` within `fun1` x2 will be undefined. It is possible to check for defined variables with the `exists` function

When a function has no left hand side argument and is called only with character string arguments, the callling syntax may be simplified `fun('a','toto','a string')` can be replaced by `fun a toto 'a string'`

### Miscellaneous

Functions are Scilab objects (with type numbers 13 or 11). They and can be manipulated (built, saved, loaded, passed as arguments,..) as other variable types.

Collections of functions can be collected in libraries. Functions which begin with `%` sign (e.g. `%foo`) are often used to overload (see `overloading`) operations or functions for new data type.

### Examples

    //inline definition (see function)
    function [x,y]=myfct(a,b)
    x=a+b
    y=a-b
    endfunction

    [x,y]=myfct(3,2)

    //inline definition (see deff)
    deff('[x,y]=myfct(a,b)',['x=a+b';
                             'y=a-b'])
    // definition in an ascii file (see exec)
    exec SCI/macros/elem/asinh.sci;

    // definition in an ascii file (see getf)
    getf SCI/macros/elem/asinh.sci;

### See Also

`function`, `deff`, `getf`, `comp`, `lib`, `getd`, `genlib`, `exists`, `varargin`, `varargout`,