Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
6.0.1 - Português

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

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

Ajuda do Scilab >> Funções > argn

argn

número de argumentos na chamada de uma função

Seqüência de Chamamento

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

Parâmetros

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.

Descrição

Esta função é usada dentro da definição de uma função. Ela fornece os números de parâmetros de entrada rhs e saída lhs passados à função quando esta é chamada. Geralmente é usada em definições de funções para lidar com parâmetros opcionais.

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.

Exemplos

Exemplos simples :

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.

Com varargin e 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.

Teste robusto da existência de argumentos de entrada:

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.

Outro uso:

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

Ver Também

  • isdef — verifica existência de variáveis
  • varargin — variável do número de argumentos de saída em uma lista de argumentos de entrada
  • varargout — variável do número de argumentos de saída em uma lista de argumentos de saída
  • macrovar — variáveis de uma função
  • function — abre definição de função
Report an issue
<< Libraries Funções deff >>

Copyright (c) 2022-2023 (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 Feb 12 19:58:45 CET 2018