Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2024.1.0 - 日本語


argn

関数コールの入力/出力引数の数を返す

呼出し手順

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

設定

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.

説明

この関数は関数定義の内部で使用されます. この関数は関数がコールされた際に関数に指定された実際の入力引数(lhs)と 出力引数(lhs) の数を出力します. 通常,関数の定義においてオプションの引数を処理する際に使用されます.

Testing the existence of a named input argument with isdef(..,"l") is more robust that with argn(). Please see 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.

Robustly testing 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

参照

  • isdef — 変数の存在を確認
  • varargin — 入力引数リストの引数の数を可変にする
  • varargout — 出力変数リストの引数の数を可変にする
  • macrovar — 関数の変数
  • function — 関数定義をオープンする
Report an issue
<< Libraries 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 Jun 17 17:54:24 CEST 2024