Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
6.0.2 - Français

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

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

Aide de Scilab >> Fonctions avancées > argn

argn

nombre effectif d'arguments d'entrée reçus / attendus en sortie d'une fonction

Séquence d'appel

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

Arguments

lhs

Nombre d'arguments de sortie attendus. Vaut 1 (au lieu de 0) si la fonction a été appelée sans argument de sortie.

rhs

Nombre d'arguments passés en entrée de la fonction Scilab considérée.

Description

Cette fonction est utilisée à l'intérieur d'une définition de fonction. Elle donne le nombre effectif (au moment de l'appel) d'arguments d'entrée rhs et de sortie lhs. Elle permet d'utiliser des arguments optionnels.

Il n'est pas possible de savoir depuis une fonction Scilab si celle-ci a été appelée sans aucun argument de sortie attendu. Le cas échéant, lhs vaut 1, au lieu de 0.
L'existence d'un argument d'entrée nommé (hors de la portée d'un éventuel varargin) peut être testée avec isdef(..,"l") de manière plus robuste qu'en utilisant argn(). Un exemple figure ci-après.

Exemples

Exemples élémentaires :

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.

Avec varargin ou/et 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.

Test robuste de l'existence d'un argument d'entrée:

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) // Les arguments passés via varargin sont toujours anonymes
 ans  =
 a passed.

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

Autre usage fréquent:

function concat=maFonction(nom, option)
  [lhs, rhs] = argn()
  if rhs <= 1 then
     option = "mon argument optionnel";
  end
  if rhs == 0 then
     error("Au moins un argument attendu");
  end
  concat = nom+" "+option;
endfunction

Voir aussi

  • isdef — vérifie si une variable existe
  • varargin — variable number of arguments in an input argument list
  • varargout — arbitrarily long list of output arguments
  • macrovar — variables d'une fonction
  • function — définition d'une fonction Scilab
Report an issue
<< Libraries Fonctions avancé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:
Thu Feb 14 15:00:00 CET 2019