Are there programming languages โ€‹โ€‹with functions with variable arguments not at the end?

Python, C ++, Scheme, and others allow you to define functions that take a variable number of arguments at the end of the argument list ...

def function(a, b, *args):
    #etc...

      

... which can be called like this:

function(1, 2)
function(1, 2, 5, 6, 7, 8)

      

etc. Are there any languages โ€‹โ€‹that allow you to perform variadic functions using an argument list somewhere else? Something like that:

def function(int a, string... args, int theend) {...}

      

With all these valid:

function(1, 2)
function(1, "a", 3)
function(1, "b", "c", 4)

      

Also, what about optional arguments anywhere in the argument list?

def function(int a, int? b, int c, int... d) {}

function(1, 2) //a=1, c=2, b=undefined/null/something, d=[]
function(1,2,3) //a=1, b=2, c=3,d=[]
function(1,2,3,4,5) //a=1, b=2, c=3, d=[4,5]

      

+1


source to share


8 answers


Future versions of Ruby (1.9 and up, Ruby 1.9 planned for late January 2009) may do this.

However, it is not always obvious which value is being bound to which parameter.

This is what Ruby 1.9 accepts:

0 or more required arguments, followed by 0 or more optional arguments, followed by 0 or more required arguments, followed by break arguments, followed by 0 or more required arguments.



Example:

def meth mand1, opt1 = :def1, o2 = :d2, *args, m2, m3
  puts %w[mand1 opt1 o2 m2 args m3].inject('') { |s, arg|
    s << "#{arg} = #{(eval arg).inspect}, "
  }.gsub /, $/, ''
end

meth :arg1, :a2, :a3
# => mand1 = :arg1, opt1 = :def1, o2 = :d2, m2 = :a2, args = [], m3 = :a3
meth :arg1, :a2, :a3, :a4
# => mand1 = :arg1, opt1 = :a2, o2 = :d2, m2 = :a3, args = [], m3 = :a4
meth :arg1, :a2, :a3, :a4, :a5
# => mand1 = :arg1, opt1 = :a2, o2 = :a3, m2 = :a4, args = [], m3 = :a5
meth :arg1, :a2, :a3, :a4, :a5, :a6
# => mand1 = :arg1, opt1 = :a2, o2 = :a3, m2 = :a5, args = [:a4], m3 = :a6
meth :arg1, :a2, :a3, :a4, :a5, :a6, :a7
# => mand1 = :arg1, opt1 = :a2, o2 = :a3, m2 = :a6, args = [:a4, :a5], m3 = :a7

      

As you can see, the required arguments are linked first, both left and right. The optional arguments then become bound, and if any arguments remain, they go into the array and bound to the remainder argument.

+1


source


BASIC has had it for a long time.

For example:

LOCATE [row%] [,[column%] [,[cursor%] [,start% [,stop%]]]]

      

This command sets the position (row%, column%) of the cursor, and also sets the size of the cursor (start%, stop%) and whether it is actually displayed (cursor%). Everything in square brackets can be omitted here, and if so, this property is not changed.

Usage example:

LOCATE , 5

      

to go to column 5 or



LOCATE 1, , 0

      

to go to the first line and make the cursor invisible.

Another command where this is seen is the PUT command for writing to files. If the middle argument (file search position) is omitted, the write occurs immediately after the previous write.

It is important to note that argument omission is only seen in built-in operations, not in user-defined procedures and functions.

In terms of implementation, it seems to make Microsoft Basic Compiler (BC) to call LOCATE:

  • For each argument:
    • If the argument is omitted, press 0
    • If passed an argument, press 1 and then press the actual value
  • Press number of arguments
  • Call a library function
+2


source


The following C ++ can do it with this syntax:

void f(int a, std::initializer_list<int> b, int c) {
    // b.begin(), b.end(), b.size() allow to access them
}

void g() { 
    f(1, { 2, 3, 4, 5 }, 2);
}

      

+2


source


Several languages โ€‹โ€‹(perl, python, and many others) can do named arguments, which is akin to doing optional arguments anywhere in a parameter list ... (Named parameters can appear in any order, and any of them can be done optionally ...) They are not exactly the same, but they are close ...

Not sure about varargs, although they can usually be replaced with an array / hash / list object ...

+1


source


Lisp keyword options might be what you're looking for. I think there is a similar convention in Ruby. See also Lisp for an overview of function parameters .

0


source


I believe PHP is counting. You can do this to mimic what you are looking for. Personally, I think it will be confusing.

function foo() {
    $args = func_get_args(); // returns an array of args
}

      

0


source


R (statistical language) also has it and it can be in the middle of the list, but there are subtle semantics.

http://cran.r-project.org/doc/manuals/R-intro.html#The-three-dots-argument

> f1 <- function(x,...,y) {  return(x+y) }
> f1(1,2)
Error in f1(1, 2) : argument "y" is missing, with no default
> f1(1,y=2)
[1] 3
> f1 <- function(x,...,y) {  return(x+y) }
> f1(1,2)
Error in f1(1, 2) : argument "y" is missing, with no default
> f1(1,y=2)
[1] 3
> 

      

0


source


It's called Rest Arguments and it can be done in C ++ and Java at least. Google "Rest Arguments" and you will find a lot of data on this subject with some examples such as functions that will pass numbers and return the average of the numbers entered, perhaps the minimum or maximum of all numbers passed. As you can see, there are many uses for such functions, I used it in code to enter data in MYSQL, so when I want to add a row, I just add the table name as the first row and the rest of all columns are names and then their data without having to sit and manually do it over and over. Good luck!

0


source







All Articles