1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
<?xml version="1.0" encoding="UTF-8"?>
<!--
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
* Copyright (C) ????-2008 - INRIA
*
* This file must be used under the terms of the CeCILL.
* This source file is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at
* http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
*
-->
<refentry xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:mml="http://www.w3.org/1998/Math/MathML"
xmlns:db="http://docbook.org/ns/docbook"
version="5.0-subset Scilab"
xml:lang="en" xml:id="functions">
<info>
<pubdate>$LastChangedDate$</pubdate>
</info>
<refnamediv>
<refname>functions</refname>
<refpurpose> Scilab procedures and Scilab objects</refpurpose>
</refnamediv>
<refsection>
<title>Description</title>
<para>Functions are Scilab procedures ("macro", "function" and "procedure"
have the save meaning).</para>
</refsection>
<refsection>
<title>Function definition</title>
<para>Usually, they are defined in files with an editor and loaded into
Scilab using the <link linkend="exec">exec</link> function or through a library (see
<link linkend="lib">lib</link> or <link linkend="genlib">genlib</link>). But They can also be
defined on-line (see <link linkend="deff">deff</link> or <link linkend="function">function</link>.
A function is defined by two components: </para>
<itemizedlist>
<listitem>
<para>a "syntax definition" part as follows:</para>
<programlisting><![CDATA[
function [y1,...,yn]=foo(x1,...,xm)
function [y1,...,yn,varargout]=foo(x1,...,xm,varargin)
]]></programlisting>
</listitem>
<listitem>
<para>a sequence of scilab instructions.</para>
</listitem>
</itemizedlist>
<para>The "syntax definition" line gives the "full" calling syntax of
this function. The <literal>yi</literal> are output variables
calculated as functions of input variables <literal>xi</literal>
and variables existing in Scilab when the function is
executed.</para>
</refsection>
<refsection>
<title>Calling function</title>
<itemizedlist>
<listitem>
<para>Usually function calling syntax is
<literal>[y1,...,yn]=foo(x1,...,xm)</literal>. 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.</para>
<para>The <link linkend="argn">argn</link> function may be used to get
the actual number of calling arguments.</para>
</listitem>
<listitem>
<para>It is possible to define function with indeterminate
number of input or output maximum number of arguments. This
can be done using the <link linkend="varargin">varargin</link>
and <link linkend="varargout">varargout</link>
keywords. See the given links for details.</para>
</listitem>
<listitem>
<para>It is also possible to use "named argument" to specify input
arguments: suppose function <literal>fun1</literal> defined as
<literal>function y1=fun1(x1,x2,x3)</literal> then it can be
called with a syntax like <literal>y=fun1(x1=33,x3=[1 2
3])</literal> within <literal>fun1</literal> x2 will be
undefined,</para>
<para> it can also be called with a syntax like
<literal>y=fun1(x1=33,y='foo')</literal>. In such a case the
<literal>y</literal> variable will be available in the context
of the function <literal>fun1</literal>. 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.</para>
<para> It is possible to check for defined variables with the
<link linkend="exists">exists</link> function.</para>
</listitem>
<listitem>
<para>When a function has no left hand side argument and is called only
with character string arguments, the calling syntax may be
simplified:</para>
<programlisting><![CDATA[
fun('a','toto','a string')
]]></programlisting>
<para>is equivalent to: </para>
<programlisting><![CDATA[
fun a toto 'a string'
]]></programlisting>
</listitem>
</itemizedlist>
</refsection>
<refsection>
<title>Miscellaneous</title>
<para>
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.</para>
<para>Collections of functions can be collected in libraries. Functions
which begin with <literal>%</literal> sign
(e.g. <literal>%foo</literal>) are often used to overload (see <link linkend="overloading">overloading</link>
) operations or functions for new data type.</para>
</refsection>
<refsection>
<title>Examples</title>
<programlisting role="example"><![CDATA[
//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;
]]></programlisting>
</refsection>
<refsection>
<title>See Also</title>
<simplelist type="inline">
<member> <link linkend="function">function</link> </member>
<member> <link linkend="deff">deff</link> </member>
<member> <link linkend="exec">exec</link> </member>
<member> <link linkend="comp">comp</link> </member>
<member> <link linkend="lib">lib</link> </member>
<member> <link linkend="getd">getd</link> </member>
<member> <link linkend="genlib">genlib</link> </member>
<member> <link linkend="exists">exists</link> </member>
<member> <link linkend="varargin">varargin</link> </member>
<member> <link linkend="varargout">varargout</link> </member>
</simplelist>
</refsection>
</refentry>
|