Using Javascript functions

What is Javascript function.

Javascript function syntax.

A function will consist of a block of statements which can
be performed several times by calling the function at different
locations in the Javascript program.

A function can perform a piece of work with or without any outside influence.

If a function need any outside influence, then the function will get these as arguments.

A function can return a value (perhaps as a result of the work it has done)
or not return any value.
The latest variant is often referred
to as a procedure in other programming languages​​.

Example:

<script type="text/javascript">
// Add is the name of the functionfunction Add(p1,p2,p3,p4,p5) { // p1,p2,p3,p4,p5 are arguments to the functionreturn p1+p2+p3+p4+p5; // return the sum of all arguments
}
// To perform a function you must specify function name followed// by comma separated arguments within parenthesesdocument.write(Add(1,2,3,4,5)+"<br>");
</script>

Javascript functions are objects.

Function name or reference can be used to access methods and properties inherited from the
'Function' object.

Example:

<script type="text/javascript">
function show(p1,p2,p3,p4,p5) { // 5 arguments to the function// 'length' is an inherited characteristic, which is made ​​unique for the function// 'length' is the expected number of arguments to the functionreturn show.length;
}
document.write(show()+"<br>");
</script>

Since functions are objects you can use functions as data.

Javascript function as data.

Javascript functions are not only a defined syntax to use in other
parts of the Javascript program, as in many other programming
tools, but also to be regarded as data.

This means that functions can be assigned to variables,
disposed of as arguments to functions,
be assigned to properties of object or to be elements of arrays with more.

Example:

<script type="text/javascript">
function Add(p1,p2,p3,p4,p5) {
return p1+p2+p3+p4+p5; // return the sum of all arguments
}
var myAdder=Add; // myAdder has now the same function reference as Add// call the function through the myAdder reference variabledocument.write(myAdder(1,2,3,4,5)+"<br>");
</script>

Javascript function without any name.

JavaScript allows functions to be defined with function literals, which is the same as defining functions unnamed.
This may seem somewhat strange, but is widely used within Javascript.

function(p1,p2,p3,p4,p5) {
return p1+p2+p3+p4+p5;
}

As you already know; functions can be assigned to variables,
passed as arguments to functions and so on.
In those cases you do not need a function name.

Example:

<script type="text/javascript">
// assigned to a variable; myAddervar myAdder=function(p1,p2,p3,p4,p5) {return p1+p2+p3+p4+p5;}
// to execute the function; you uses the variable, // which is a reference to the functiondocument.write(myAdder(1,2,3,4,5)+"<br>");
// assign to an array locationvar myarr=[function(p1,p2,p3,p4,p5) {return p1+p2+p3+p4+p5;}];
// to execute the function; you uses the array location as a functiondocument.write(myarr[0](1,2,3,4,5)+"<br>");
// assign to an objectvar myobj={"Adder":function(p1,p2,p3,p4,p5) {return p1+p2+p3+p4+p5;}};
// to execute the function; you uses the object member as a functiondocument.write(myobj.Adder(1,2,3,4,5)+"<br>");
</script>

Passing arguments to functions.

Primitive data types are sent as arguments to functions by value​​. This means that
you will receive a copy of the original data and can
not make any changes to the original data in such functions.

Object types are sent as arguments to functions by reference​​.
This means that you can make changes to the original data in such functions.