Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
6.1.1 - 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 assigned at the function call.

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.

Testing the existence of a named input argument with isdef(..,"l") is more robust that with argn(). Please see examples.

When the caller function has either no output argument or only varargout, the (lhs) is set to 0. Otherwise, the number of expected arguments is set.

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);

With varargin and varargout:

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();

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)

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

History

VersionDescription
6.1.0The lhs argument is set to zero when the caller function has no output assignement.
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:
Mon Jan 03 14:23:44 CET 2022