In this article we will learn about functions in JavaScript. We will learn how to declare a function and how to use it with examples.
Introduction
A function is a block of code which can be executed again and again. Functions make the code modular which improves maintenance. Functions in JavaScript are similar to functions in C language. A function definition is as shown below:
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);
In JavaScript functions are objects. So, variables that reference them can be treated as object references. Example for function definition and function call is given below:
//Function Definition
function add(x, y)
{
return x+y;
}
//Function Call
addref = add; //Here addref is a reference to the add(function) object
add(10, 20);
addref(20, 30); //This is also valid
In JavaScript, a function definition must occur before a function call. So, it is wise to define a function in the head section of the page.
Local Variables
The scope of a variable denotes the range of statements over which the variable is visible. In JavaScript we have two kinds of variables: global variables and local variables.
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.
Parameters
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
In JavaScript, parameters are passed by value. Even though object references are being passed, references itself are passed as a value.
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply