It just sounds cool, doesn't it? But what are they?
I think we all know what a normal method signature looks like, regardless of what your favorite language might be. If you don’t, a method signature refers to the pattern of a functions definition. It consists of the name, sometimes an access modifier if you’re language supports them, a return type for strongly typed languages, and the number and types of the arguments you can pass in.
For example in C#, a method signature looks like this:
We can see the name, preceded by an access modifier, in this case public, the ubiquitous set of parenthesis that tells us this is a function or method. The parenthesis contains an optional list of arguments which are variables you can pass into the function to allow it to do its work. In the case of C#, a strongly typed language, we need the types for the arguments.
If your working in Java, the method signature pattern is almost indistinguishable from C#.
Here it is in python 3.
I’m not using a class here, so there is no access modifier is needed. You don’t normally define the return type of the function in Python, but the rest of this method signature is identifiable. We have a list of arguments enclosed in parenthesis and I’ve included the optional hints for python to work effectively with types
No matter what language you use, there are rules:
1). Your method signature must be unique within it’s defined scope.
3). Don’t name your functions the same name you use a variable in the same scope.
So now we have an idea of what a method signature and one idea seems clear. Method signatures may include a fixed number of arguments whether its none, one, five, or twenty-five, at which point you’d probably want to refactor. The point is, the number of arguments is fixed by the method signature.
But what if it wasn’t? What if you could pass in an indeterminate number of arguments and have your function operate on all of them?
Here’s some C# code and here is a variadic function definition.
It uses the params keyword inside the parenthesis and it requires you to set a type. Like collections in C#, you have to use the same type of arguments. So you can pass in a list of ints, floats, strings, or objects, but the list members, the things in the list, must be the same type.
In Python 3, we see variadic functions defined as the first argument in a method signature.
The *args is the pythonic way of defining variadic functions. If you want to make a variadic function in python, the variadic argument definition should be the first one in the method signature.
In ES6, the variadic capability is called rest. Not to be confused with the acronym for representational state transfer, a rest parameter allows you to pass in an indeterminate number of arguments with one caveat: it must be the last variable defined in the method signature.
Variadic functions can be a very useful addition to any data science or computational workflow, and now you’re ready to use them in your language of choice. What will you use them for?