update CHANGES and help pages after c1dbf528
[scilab.git] / scilab / modules / functions / help / en_US / argn.xml
index ba28b5b..f5130a1 100644 (file)
 <?xml version="1.0" encoding="UTF-8"?>
-<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" xmlns:scilab="http://www.scilab.org" xml:lang="en" xml:id="argn">
+<!--
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) ????-2008 - INRIA
+ * Copyright (C) 2018 - Samuel GOUGEON
+ *
+ * Copyright (C) 2012 - 2016 - Scilab Enterprises
+ *
+ * This file is hereby licensed under the terms of the GNU GPL v2.0,
+ * pursuant to article 5.3.4 of the CeCILL v.2.1.
+ * This file was originally licensed under the terms of the CeCILL v2.1,
+ * and continues to be available under such terms.
+ * For more information, see the COPYING file which you should have received
+ * along with this program.
+ *
+ -->
+<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" xmlns:scilab="http://www.scilab.org"
+        xml:lang="en" xml:id="argn">
     <refnamediv>
         <refname>argn</refname>
-        <refpurpose>Returns the number of input/output arguments in a function call</refpurpose>
+        <refpurpose>Returns the actual number of input/output arguments in a function call</refpurpose>
     </refnamediv>
     <refsynopsisdiv>
-        <title>Calling Sequence</title>
-        <synopsis>[lhs [,rhs] ]=argn()
-            lhs=argn(1)
-            rhs=argn(2)
+        <title>Syntax</title>
+        <synopsis>
+            [lhs, rhs] = argn()
+            lhs = argn(1)
+            rhs = argn(2)
         </synopsis>
     </refsynopsisdiv>
+    <refsection role="arguments">
+        <title>Arguments</title>
+        <variablelist>
+            <varlistentry>
+                <term>lhs</term>
+                <listitem>
+                       <para>Number of expected Left-Hand-Side output arguments assigned at the function call.
+                </para>
+                </listitem>
+            </varlistentry>
+            <varlistentry>
+                <term>rhs</term>
+                <listitem>
+                <para>Number of Right-Hand-Side input arguments provided in the function call.
+                </para>
+                </listitem>
+            </varlistentry>
+        </variablelist>
+    </refsection>
     <refsection>
         <title>Description</title>
         <para>
             This function is used inside a function definition.
-            It gives the number of actual inputs arguments (<varname>rhs</varname>)
-            and output arguments (<varname>lhs</varname>) passed to the function when the function is 
-            called. It is usually used in function definitions to deal with 
-            optional arguments.
+            It gives the number of actual inputs arguments (<varname>rhs</varname>) and output arguments (<varname>lhs</varname>) passed to the function when the function is called. It is usually used in function definitions to deal with optional arguments.
         </para>
+        <warning>
+            Testing the existence of a named input argument with <literal>isdef(..,"l")</literal>
+            is more robust that with <literal>argn()</literal>. Please see examples.
+        </warning>
         <para>
-            Note that <varname>lhs</varname> is always greater than or equal to 1. That is to say, even if a function
-            is called without having mentioned an output argument, <varname>lhs</varname> will be equal to 1.
+            When the caller function has either no output argument or only <varname>varargout</varname>, the (<varname>lhs</varname>) is set to <code>0</code>. Otherwise, the number of expected arguments is set.
         </para>
     </refsection>
     <refsection>
         <title>Examples</title>
-        <programlisting role="example"><![CDATA[ 
-function concat=myOwnFunction(name,optional)
-  [lhs,rhs]=argn(0);
-  disp(lhs);
+        <para>Simple examples:</para>
+        <programlisting role="example"><![CDATA[
+function [res, res2] = test(a, b)
+  [lhs, rhs] = argn()
+  [res, res2] = ("abc", %pi);
+  disp([lhs rhs])  // <<<<<<<<<<<
+endfunction
+
+test();
+test(4.321);
+test(3, -1);
+test(3, -1, a=0);
+test(3, -1, c=8);
+
+out1 = test();
+[o1, o2] = test(%pi);
+ ]]></programlisting>
+    <para/>
+    <para>With varargin and varargout:</para>
+        <programlisting role="example"><![CDATA[
+function [varargout] = disp_argn(varargin)
+  varargout = list("abc",%i);
+  [lhs, rhs] = argn()
+  disp([lhs rhs])  // <<<<<<<<<<<
+endfunction
+
+function [res, varargout] = disp_argn_with_args(a, varargin)
+  res = "abc";
+  varargout = list(%i);
+  [lhs, rhs] = argn()
+  disp([lhs rhs])  // <<<<<<<<<<<
+endfunction
+
+// varargin
+disp_argn(1);
+disp_argn_with_args(1);
+disp_argn(1, 2);
+disp_argn_with_args(1, 2);
+disp_argn(1, 2, 3);
+disp_argn_with_args(1, 2, 3);
+
+// varargout
+out1 = disp_argn();
+out1 = disp_argn_with_args();
+[o1, o2] = disp_argn();
+[o1, o2] = disp_argn_with_args();
+[o1, o2, o3] = disp_argn();
+[o1, o2, o3] = disp_argn_with_args();
+ ]]></programlisting>
+    <para/>
+    <para>Robust test of the existence of input arguments:</para>
+        <programlisting role="example"><![CDATA[
+function res = test(a, b, varargin)
+    res = ""
+    if isdef("a","l")
+        res = "a passed."
+    end
+    if isdef("b","l")
+        res = res + " b passed."
+    end
+    if isdef("c","l")
+        res = res + " c passed."
+    end
+endfunction
+clc
+test()
+test(4.321)
+test(4.321, %z)
+test(b=3)
+test(c=3)
+test(-1, c=3)
+test(-1, a=2, c=3)
+test(b=-1, a=2, c=3)
+ ]]></programlisting>
+    <para/>
+    <para>Another usage:</para>
+    <programlisting role="example"><![CDATA[
+function concat = myOwnFunction(name,optional)
+  [lhs, rhs] = argn()
   if rhs <= 1 then
-        optional="my Optional value";
+     optional="my Optional value";
   end
   if rhs == 0 then
-        error("Expect at least one argument");
+     error("Expect at least one argument");
   end
   concat=name+" "+optional;
 endfunction
  ]]></programlisting>
     </refsection>
     <refsection role="see also">
-        <title>See Also</title>
+        <title>See also</title>
         <simplelist type="inline">
             <member>
-                <link linkend="function">function</link>
+                <link linkend="isdef">isdef</link>
             </member>
             <member>
                 <link linkend="varargin">varargin</link>
             </member>
+            <member>
+                <link linkend="varargout">varargout</link>
+       </member>
+            <member>
+                <link linkend="macrovar">macrovar</link>
+            </member>
+            <member>
+                <link linkend="function">function</link>
+            </member>
         </simplelist>
     </refsection>
+    <refsection>
+           <title>History</title>
+           <revhistory><revision><revnumber>6.1.0</revnumber><revremark>The <code>lhs</code> argument is set to zero when the caller function has no output assignement.</revremark></revision></revhistory>
+    </refsection>
 </refentry>