Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2025.0.0 - English


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 using the exec function or through a library (see lib or genlib). But they can also be defined on-line (see deff or function. 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 or set.

    The nargin and nargout variables can be used to obtain the actual number of input and output arguments.

    _ can be used to ignore output argument(s).

    [_, b, _, d] = regexp(..."); in this case, only the 2nd and 4th parameters will be assign to output variables

  • It is possible to define function with indeterminate number of input or output maximum number of arguments. This can be done using the varargin and varargout keywords. See the given links for details.

  • 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 can be called with a syntax like y = fun1(x1=33, x3=[1 2 3]) within fun1 x2 will be undefined.

    It can also be called with a syntax like y = fun1(x1=33, y='foo'). In such a case the y variable will be available in the context of the function fun1.

    Note that the maximum number of argument must be less or equal to the number of formal input argument used in the function syntax part.

    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 calling syntax may be simplified:

    fun('a','toto','a string')

    is equivalent to:

    fun a toto 'a string'

Miscellaneous

Functions are Scilab objects (with type numbers 13 or 11). And they 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.

Note that a function name must respect some syntax rules (see names).

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/modules/elementary_functions/macros/asinh.sci;

See also

  • function — opens a function definition
  • deff — in-line definition of a (anonymous) function in Scilab language
  • exec — script file execution
  • lib — loads a library of Scilab functions and variables, and sets its name
  • getd — Load all functions defined in a directory
  • genlib — builds a library from a set of *.sci files defining functions in a given directory
  • exists — checks variable existence
  • varargin — variable number of arguments in an input argument list
  • varargout — arbitrarily long list of output arguments
  • names — Naming rules for variables and functions
Report an issue
<< boolean types hypermatrices >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Thu Oct 24 11:13:05 CEST 2024