Skip to content
Akin C edited this page May 20, 2018 · 17 revisions

Welcome to the Javascript-variadic-functions-and-arguments- wiki!

This repository is part of a larger project!

◀️ PREVIOUS PROJECT| NEXT PROJECT ▶️


A function in Javascript could be constructed with different number of parameters:

//One Parameter
function One(a)
{
    return a+a;
}

//Three Parameters
function Two(a,b,c)
{
    return a+b+c;
}

/*Checking the number of parameters in a function*/
One.length;//Outputs 1
Two.length;//Outputs 3

📕 The number of arguments that a function could accept is called arity.

The following example should note that argument is not the same as parameter.

//"a" and "b" are parameters
function anyFunction(a,b)
{
    return a + b;
}

var anyVariable = 2;

//The value "1" and "anyVariable" are arguments
anyFunction(1,anyVariable);

All the functions listed above have a fixed arity (in other words, they have fixed numbers of arguments they can take).

📕 Even though number of arguments and parameters are in general dependend , in Javascript there is an object called arguments which should be able to give a function the ability to take indeterminate number of arguments. As the folllowing example should show:

function Concatenating()
{
    var concat = "";
    
    //"arguments" grants access to the function`s arguments    
    for(var i  = 0; i < arguments.length; i++)
    {	
        //Converts the value in the list to type "string"
        concat += String(arguments[i]);
    }
	
    return concat;
}

//Outputs "42 hours"
Concatenating("4", 2, " hours");

In the listing above the function Concatenating is called with three arguments even though it has no parameters. This is possible, because of the implicit local variable arguments which should be usable in every javascript function.

📕 This kind of function is also known as variadic function or also known as variable-arity function.

It seems that it is a good idea that whenever a variable-arity function is provided, one should provide also a fixed-arity version of that function.

This could be done by implementing the variadic function as a small wrapper which contains the fixed-arity version in its definition. An example follows:

//Fixed-arity version - Takes an array as argument
function Concatenating(a)
{
    var concat = "";
        
    for(var i  = 0; i < a.length; i++)
    {	
        //Converts the value in the list to type "string"
        concat += String(a[i]);
    }
	
    return concat;
}

//Variadic version - Takes seperated arguments
function Concate()
{
   return Concatenating(arguments);
}

//Outputs "42 hours"
Concatenating(["4", 2, " hours"]);
//Outputs "Hello World"
Concate("Hello", " W", "or", "ld");

The reason behind this, it seems, is to avoid using the javascript`s method apply and thus improving the code readability.

Content

The user interaction part, after inserting some inputs, could look like the content as seen below by starting "index.html" in a web browser.

ERROR: No image found!

  • 🅱️ utton "GENERATE" generates an NxM matrix and activates a complete specific area which is pointed with a blue arrow in the picture

  • 🅱️ utton "FIXED" uses the fixed-arity function version and calculates the average value given by the matrix

  • 🅱️ utton "VARIADIC" uses the variadic function version and calculates the average value given by the textarea`s function calcAverage. All it needs are the values as arguments given to calcAverage.

The colored areas are partly for a better readability in the wiki and are not part of the content. To use the project just download the files and execute "index.html". Note that all files (folder "wiki_images" excluded) should be placed in the same folder so that the functionality of the code is guaranteed.

Clone this wiki locally