Advanced Java and Web Technologies for JNTUK
Blog providing beginner tutorials on different web technologies like HTML, CSS, Javascript, PHP, MYSQL, XML, Java Beans, Servlets, JSP and AJAX
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Javascript. No Comments on Functions in Javascript

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.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

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.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *