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

Change language to:
Français - 日本語 - Português - Русский

Please note that the recommended version of Scilab is 2025.0.0. This page might be outdated.
See the recommended documentation of this function

Scilab Help >> Advanced functions > argn

argn

Returns the actual number of input/output arguments in a function call

Syntax

[lhs, rhs] = argn()
lhs = argn(1)
rhs = argn(2)

Arguments

lhs

Number of expected Left-Hand-Side output arguments. Is set to 1 when no output argument is expected.

rhs

Number of Right-Hand-Side input arguments provided in the function call.

Description

This function is used inside a function definition. It gives the number of actual inputs arguments (rhs) and output arguments (lhs) passed to the function when the function is called. It is usually used in function definitions to deal with optional arguments.

There is no way to detect whether a Scilab function is called without expected output argument. In this case, lhs returns 1.
Testing the existence of a named input argument with isdef(..,"l") is more robust that with argn(). Please see examples.

Examples

Simple examples:

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);
--> test();
   1.   0.

--> test(4.321);
   1.   1.

--> test(3, -1);
   1.   2.

--> test(3, -1, a=0);
Wrong number of input arguments.

--> test(3, -1, c=8);
Wrong number of input arguments.

--> out1 = test();
   1.   0.

--> [o1, o2] = test(%pi);
   2.   1.

With varargin and varargout:

function [res, varargout]=test(a, varargin)
  res = "abc";
  varargout = list(%i);
  [lhs, rhs] = argn()
  disp([lhs rhs])  // <<<<<<<<<<<
endfunction

test();
test(4.321);
test(3, -1);
test(3, -1, a=0);
test(3, -1, 8);

out1 = test();
[o1, o2] = test(%pi);
[o1, o2, o3] = test(%pi);
--> test();
   1.   0.

--> test(4.321);
   1.   1.

--> test(3, -1);
   1.   2.

--> test(3, -1, a=0);
   1.   3.

--> test(3, -1, 8);
   1.   3.

--> out1 = test();
   1.   0.

--> [o1, o2] = test(%pi);
   2.   1.

--> [o1, o2, o3] = test(%pi);
   3.   1.

Robust test of the existence of input arguments:

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)
--> test()
 ans  =

--> test(4.321)
 ans  =
 a passed.

--> test(4.321, %z)
 ans  =
 a passed. b passed.

--> test(b=3)
 ans  =
  b passed.

--> test(c=3)
 ans  =
  c passed.

--> test(-1, c=3)
 ans  =
 a passed. c passed.

--> test(-1, a=2, c=3) // argins in varargin are/become always anonymous
 ans  =
 a passed.

--> test(b=-1, a=2, c=3)
 ans  =
 a passed. b passed.

Another usage:

function concat=myOwnFunction(name, optional)
  [lhs, rhs] = argn()
  if rhs <= 1 then
     optional="my Optional value";
  end
  if rhs == 0 then
     error("Expect at least one argument");
  end
  concat=name+" "+optional;
endfunction

See also

  • isdef — checks variable existence
  • varargin — variable number of arguments in an input argument list
  • varargout — arbitrarily long list of output arguments
  • macrovar — variables of function
  • function — opens a function definition
Report an issue
<< Libraries Advanced functions deff >>

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 Feb 14 14:57:46 CET 2019