function functionName(param1, param2, ... , paramN)
//Body of the function
return; //This is optional
The function definition consists of function header and the body of the function (a compound statement). The function header consists of function keyword, function name followed by parameters (if any) enclosed in round brackets.
The function body consists a set of statements and an optional return statement that are executed when the function is called. A function call is as shown below:
functionName(param1, param2, … , paramN);
function add(x, y)
addref = add; //Here addref is a reference to the add(function) object
addref(20, 30); //This is also valid
Variables that are declared implicitly (without var) outside a function or inside a function definition have global scope. Variables that are declared explicitly (with var) outside (including compound statements) a function also have global scope. Variables that are declared explicitly inside a function definition have local scope.
When a global variable and a local variable have the same name, the local variable hides the global variable.
The parameters available in the function call are known as actual parameters while the parameters in the function definition are known as formal parameters.
When the formal parameters are more than the actual parameters, remaining formal parameters are set to undefined. When the formal parameters are less than the actual parameters, remaining actual parameters are truncated.
The actual parameters passed to a function can be accessed through the property array, arguments. So, even when the formal parameters are less than the actual parameters, all the actual parameters are directly accessible through arguments property.
We can use the arguments property as shown below:
arguments.length //Gives the number of actual parameters
arguments[n] //Access the nth actual parameter