Python, C++, Scheme, and others all let you define functions that take a variable number of arguments at the end of the argument list…
def function(a, b, *args): #etc...
…that can be called as followed:
function(1, 2) function(1, 2, 5, 6, 7, 8)
etc… Are there any languages that allow you to do variadic functions with the argument list somewhere else? Something like this:
def function(int a, string... args, int theend) {...}
With all of 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]
Future versions of Ruby (1.9 and up, Ruby 1.9 is scheduled to released at the end of January, 2009) can do this.
It is however not always obvious which value gets bound to which parameter.
This is what Ruby 1.9 accepts:
0 or more mandatory arguments followed by 0 or more optional arguments followed by 0 or more mandatory arguments followed by rest arguments followed by 0 or more mandatory arguments.
Example:
As you can see, mandatory arguments are bound first, from both the left and the right. Then optional arguments get bound and if any arguments are left over, they get bundled up in an array and bound to the rest argument.