help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Variable num args. question


From: Geraint Paul Bevan
Subject: Re: Variable num args. question
Date: Wed, 22 Dec 2004 22:09:22 +0000
User-agent: Mozilla Thunderbird 0.5 (X11/20040306)

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

D Goel wrote:
| hi,
|
| Inside function f1, how do i make a call to f2 with all the arguments
| that f1 was given?  with the num. of arguments being variable?
|
| The lisp equivalent being:
|
| (defun f1 (&rest args)
|       (some work ...)
|         (apply 'f2 args))
|


You can use the varargin variable to capture variable numbers of arguments:


function y = f1( varargin )
~  f2( varargin{:} )
endfunction


function y = f2( arg1, arg2, arg3, arg4 )
~  switch nargin
~    case 4
~      arg1, arg2, arg3, arg4
~    case 3
~      arg1, arg2, arg3
~    case 2
~      arg1, arg2
~    case 1
~      arg1
~  endswitch
~  y = nargin
endfunction


octave> f1( 2, 5, 7 )
arg1 = 2
arg2 = 5
arg3 = 7
y = 3
ans = 3


If you don't want to capture all of the arguments, you can name some of
them before using varagin:


function y = f1( arg1, varargin )
~  f2( varargin{:} )
endfunction


octave> f1( 2, 5, 7 )
arg1 = 5
arg2 = 7
y = 2
ans = 2



- --
Geraint Bevan
http://homepage.ntlworld.com/geraint.bevan

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iEYEARECAAYFAkHJ8JIACgkQcXV3N50QmNOu6ACffUQXk9y5/tu8EotvJ2Ap08Tv
IqAAnigT59c4Ckm4F6/DdJbW597aP7ux
=Cni3
-----END PGP SIGNATURE-----



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]