Introduced in Verge 3.1, VC has variadic functions, also called variable-argument functions or 'varargs' function. Basically what this allows you to do is create a function that can accept any number of arguments, and handle these arguments as an indexable array. It's an advanced feature, but can be used to do all sorts of clever things.
So an example of how to declare a variable-argument function, is done like so:
void MyFunc(... args) { }
"..." is a special variable type identifier, which denotes that the parameter being declared will accept any number of arguments.
Since ... just refers to the variable type it is possible to rename the variable to something other than "args" else if you'd prefer another name:
void MyFunc(... arguments) { }
To call a varargs function, you simply pass a comma separated list of all the arguments you're supplying the function. Note that varargs parameters can accept strings, ints, or absolutely nothing. All of the following are valid calls on the above MyFunc() function
MyFunc(); MyFunc(1, "hello"); MyFunc(1, 3, 4, 4424, 3); MyFunc("bob");
If we only have a varargs parameter in our function, we could still get away with passing no arguments, or perhaps less than our function logic would want to expect.
But, you can be relieved, since it is still possible to have required arguments that must be passed before the optional varargs list. The only syntactic requirement is that you always declare required arguments before a varargs argument (that is to say the varargs parameter can only be the final parameter declared, and there can only be one varargs variable per function, for obvious reasons)
void MyFunc(string name, int count, ... args) { }To actually use the varargs parameters, you refer to variable (like "args") by name and then access its members like a struct, by writing a "." (dot) between the variable name and the members. Variable argument lists have the following members (change "args" for whatever the name of your parameter is):
int args.length; (read-only) // The number of varargs arguments int args.int[argument]; (read/write) // For integer valued arguments string args.string[argument]; (read/write) // For string valued arguments int args.is_int[argument]; (read-only) // For checking if an argument is an integer type int args.is_string[argument]; (read-only) // For checking if an argument is a string typeIn the case an argument is not of the type you're checking, it'll return "" for strings or 0 for ints, which is a lot nicer than blowing up in your face. Here is a simple example that adds a bunch of numbers together:
int AddNumbers(... args) { int i, sum; for(i = 0; args.length; i++) { if(args.is_int[i]) { sum += args.int[i]; } } return sum; }Another thing to note is that the varargs parameter may be passed down to further functions, so you could write something like this AFTER the above example:
int FourPlusNumbers(... args) { return AddNumbers(4, args); // Will pass 4, followed by all arguments passed to this call. }Potentially nifty function chaining things can be done as a result of this. I'll leave the creativity up to you.