The input number of one function

I want to let Julia to known the number of parameters which I have provided in one function. Like nargin in MATLAB. What should I do?

It’s hard to imagine a case where nargin would be a better choice than just using Julia’s multiple dispatch and keyword arguments. Can you provide a short pseudocode example of what you’re trying to do?

2 Likes

I second @stillyslalom. However it could be done with

function f(args...)
    nargin = length(args)
    # now what?
end
3 Likes

For example,I have such a function:

function A(file1,file2,file3,img1,img2,img3,...)
# I need to known how many pictures and files it should be offered. 
   if (nargin < 6)
      file1->img1
      file1<-img2
   end
   if (nargin <10)
      file1->img1
      file1<-img2
      file2->img3
      file2<-img4
   end
...
end

I think you should

function a(files, images)
  for i=1:2:length(files)
    ...
  end
end

if you really want to pass them in a separate arguments

function a(args...)
  files = args[1:2:end÷2]
  imgs = args[2:2:end÷2]
  for i=1:length(files)
    ..
  end
end
4 Likes

Thank you very much.