What is the Julia equivalent of the MATLAB function: nargin()

1,342 views
Skip to first unread message

Adrian Torrie

unread,
Apr 1, 2014, 7:14:18 AM4/1/14
to julia...@googlegroups.com
As the title says, is there an equivalent?

If not, would setting optional args to a ridiculous value, e.g. -999999999999, and then  testing for which args have/don't have that value suffice to determine the number of args passed through.

I'm trying to port some MATLAB code I found on the interwebs.

Jacques Rioux

unread,
Apr 1, 2014, 7:33:44 AM4/1/14
to julia...@googlegroups.com

I don't know Matlab but I believe what you are after is length as used below.

function demo (x...)
     length (x)
end

demo(3, 4, 5)

3

Ethan Anderes

unread,
Apr 1, 2014, 10:29:32 AM4/1/14
to julia...@googlegroups.com
Hi Adrian:

Not sure how long you've been working with Julia but I also was looking for nargin from Matlab when I recently converted to Julia. I figure I would post this for other newbies in my situation. I had a lot of Matlab code like the following

function y = foo(x, z)
if nargin > 1
... do something with x and z ..
else
.... do something with x ...
end
end

Then I realized the multiple dispatch approach of Julia make this much more clean. So in julia I would write

function foo(x)
.... do something with x ...
end
function foo(x, z)
.... do something with x and z ..
end


Cheers,
Ethan


John Myles White

unread,
Apr 1, 2014, 10:42:45 AM4/1/14
to julia...@googlegroups.com
+1 for Ethan’s approach

Any time that you would have used conditionals in a function to change behavior based on the number of arguments or their types, the Julian approach is to use multiple dispatch instead.

— John

J Luis

unread,
Apr 1, 2014, 10:59:44 AM4/1/14
to julia...@googlegroups.com
Yes, the variable input arguments is easy for us (matlabers) to adapt for, but I really miss (or didn't find the replacement yet) is the conditional behavior depending on the number of outputs. That is

if (nargout == 2)
  ...
elseif (nargout == 3)
 ...

John Myles White

unread,
Apr 1, 2014, 11:04:12 AM4/1/14
to julia...@googlegroups.com
Multiple dispatch handles that in the same way that Ethan’s code demonstrates.

In my experience, nargin usage is often a pattern for working around the absence of default arguments, which Julia has.

 — John

Steven G. Johnson

unread,
Apr 1, 2014, 11:24:08 AM4/1/14
to julia...@googlegroups.com


On Tuesday, April 1, 2014 10:59:44 AM UTC-4, J Luis wrote:
Yes, the variable input arguments is easy for us (matlabers) to adapt for, but I really miss (or didn't find the replacement yet) is the conditional behavior depending on the number of outputs. That is

Julia functions do not know the number of outputs, and cannot dispatch on that.  Usually you write different functions for different numbers of outputs, e.g. eigvals vs. eigs. 

Steven G. Johnson

unread,
Apr 1, 2014, 11:26:45 AM4/1/14
to julia...@googlegroups.com
Note that default arguments in Julia can simplify this further.  Often you have Matlab code like

function y = foo(x, z)
     if nargin < 2
         z = ...default value...
     end
     ....
end

Which in Julia can be simplified to

function foo(x, z = ...default value...)
    ...
end
 
This is equivalent to declaring two functions, foo(x,z) and foo(x) = foo(x, ...default value...)

Oliver Lylloff

unread,
Apr 2, 2014, 4:41:21 AM4/2/14
to julia...@googlegroups.com
Are the default values then considered in a global scope? Not sure I fully understand the doc: http://docs.julialang.org/en/latest/manual/functions/#evaluation-scope-of-default-values

function foo(x,z=1)
    ...    # z in global scope?
end

function foo2(x) 
    z=1     # Local scope
    ...
end

- Oliver

Ivar Nesje

unread,
Apr 2, 2014, 5:17:28 AM4/2/14
to julia...@googlegroups.com
No, the difference in scope is for the right side of the = sign.

Default parameters is only fancy syntax for declaring multiple methods at once.

function foo(x, z = a)
## calculation using x and z
end

is equivalent to

function foo(x,z)
## calculation using x and z
end

foo(x) = foo(x, a)

In the second definition `a` is accessed as a global variable, but when `a` is a immutable and constant, you don't have to care.

Ivar
Reply all
Reply to author
Forward
0 new messages