You are on page 1of 5

How to write a function in JavaScript

A JavaScript function is a block of code that consists of a set of


instructions to perform a specific task. A function can also be considered as a
piece of code that can be used over again and again in the whole program, and it
also avoids rewriting the same code. It also helps programmers/coders to divide
a huge program into several small functions.

Types of Functions

There are two types of functions in JavaScript like other programming


languages such c, c++, and java etc.

o Pre-defined Functions
o User-defined Functions

Here we are going to learn, how to write a user-defined function in


JavaScript:

To create a function in JavaScript we have to use the "function " keyword


before writing the name of our function as you can see in given syntax:

Syntax of creating function

JavaScript Function Syntax


A JavaScript function is defined with the function keyword, followed by
a name, followed by parentheses ().

Function names can contain letters, digits, underscores, and dollar signs
(same rules as variables).

The parentheses may include parameter names separated by commas:


(parameter1, parameter2, ...)

The code to be executed, by the function, is placed inside curly


brackets: {}

function name(parameter1, parameter2, parameter3)


{
  // code to be executed
}
Function parameters are listed inside the parentheses () in the function
definition.

Function arguments are the values received by the function when it is


invoked.

Inside the function, the arguments (the parameters) behave as local


variables.

Before using a function or we can say before calling a function in our program
we have to define its definition in between the curly braces. As per your
requirement, we can leave the parameter list blank as you can see in the syntax
given above.

Example

<script types="text/javascript">  
<!--  
function Hello(){  
alert("Hi, there");  
}  
//-->  
</script>  
How to call the function
We can call the function when we want to use the function in the program by
writing its name as you can see below:

Hello();  

Let's see a program in which, we will create a function and use it in the
program.

<html>  
<head>  
    <title>Functions!!!</title>  
    <script type="text/javascript">  
function myfirstFunction()  
      {  
    document.write("This is just a simple user-defined  function.<br />");  
      }  
        myfirstFunction();  
    </script>  
</head>  
<body>  
</body>  
</html>  

In the above-given program, we have created a function with


"myfirstFunction" name and in the definition of this function, we displayed a
message "This is just a simple user-defined function" by using the
document.write(); function. To print that message, we first need to call the
function as you can see in program.

To call the function somewhere else in the script, we just have to write its name
as you can see in the given example:

Example
<html>  
<head>  
<script type = "text/javascript">  
functionsayhi() {  
document.write ("Hello there!");  
         }  
</script>  
  
</head>  
  
<body>  
<p>Click the given button to call the function</p>  
<form>  
<input type = "button" onclick = "sayhi()" value = "Say Hello">  
</form>  
  
</body>  
</html> 
Function With Parameters

The function we have used in our program is without parameters (or we


can say parameter less) because we have not given any parameter in the
parameters list and left it empty. But we can also use parameters with function
and we can use any number of parameters in our function but they must be
separated by comma. These parameters are captured by the function and later
any operation can be performed on these parameters inside the function.

Syntax of creating a function with parameters

function functionname( parameter1,parameter2,....parameterN)  
{  
  
Lines of code to be executed/set of instructions to be executed in order to per
form a specific task.  
}  

We can understand how to use parameters with function more easily with the
help of an example:

<html>  
<head>  
<script type = "text/javascript">  
Function sayHello(name, age,gender) {  
document.write (name + " is " + age + " years old" + " and gender is " + gender);  
  }  
</script>  
</head>  
  
<body>  
<p>Click the following button to call the function</p>  
<form>  
<input type = "button" onclick = "sayHello(' Isabella', 23,'female')" value = "Say H
ello">  
</form>  
</body>  
</html>  

In this program, we created a function named "sayHello ()" with three parameters:
name, age, and gender, and defined it in the head section of the HTML document. To
use this function, we also created a button using the form tag in the program's body
section and pass the values as arguments. When the user clicks that button, our
function is called and gets executed.

Function with return statement

In JavaScript, we can make functions that are able to return a value. To


create such type of function, we have to use the return statement, but it must be
the last statement in the body of the function (or in the definition of the
function). Another essential thing to remember is that we can use only one
return statement in a function. If we try to use several return statements in a
function, only one return statement is considered, which is reached by the
program's control first.

Syntax of function with return statement

Function functionname(arg1, arg2)  
  
{  
  
  //Set of instructions to be executed  
  
return val1;  
  
}  

You might also like