JavaScript functions

A function is a group of reusable code that can be called anywhere in your program. This makes it unnecessary to write the same code over and over again. It helps programmers write modular code. Functions allow a programmer to divide a large program into small and manageable functions.

A function is a group of reusable code that can be called anywhere in your program. This makes it unnecessary to write the same code over and over again. It helps programmers write modular code. Functions allow a programmer to divide a large program into small and manageable functions.

Like any other high-level programming language, JavaScript also supports all the features needed to write modular code using functions. You have seen functions like alert () and write () in the previous chapters. We have used these functions many times, but they have only been written in core JavaScript.

JavaScript also allows us to write our own functions. This section explains how to write your own functions in JavaScript.

Function definition

Before we use a function, we need to define it. The most common method for defining a function in JavaScript is by using the Function keyword, followed by a unique function name, a list of parameters (which may be blank), and a block of statements surrounded by the brackets.

Syntax

Simple syntax is as follows:

  type = "text/javascript" >  

For example

Try the following example. It defines a function called sayHello that doesn't take any parameters:

  type = "text/javascript" >  

Call a function

To call a function somewhere after the script, you will simply need to write the name of the function like the following code:

  type = "text/javascript" > function sayHello () { document . write ( "Hello there!" ); } 
 Click the following button to call the function  type = "button" onclick = " sayHello () " value = "Xin chao!" > 
 Use different text in write method and then try. 

Result

Following the above method will produce results

Parameter of function

To this chapter, we saw functions without parameters. But there is a convenience when passing different parameters while calling a function. These passed parameters can be captured inside that function and any operation can be performed on those parameters. A function can take multiple parameters separated by commas.

For example

Try the following example. We edited the sayHello function here. Now it takes two parameters.

  type = "text/javascript" > function sayHello ( name , age ) { document . write ( name + " is " + age + " years old." ); } 
 Click the following button to call the function  type = "button" onclick = " sayHello ( 'Zara' , 7 ) " value = "Xin chao!" > 
 Use different parameters inside the function and then try. 

Result

Following the above function will produce the result

Return command

A JavaScript function can have an arbitrary r eturn command. This command is required if you want to return a value from a function. This command should be the last command of a function.

For example, you can pass two numbers in a function and then you can expect that function to return the result of multiplying those two numbers in the program that calls your function.

For example

Try the following example. It defines a function that takes two parameters and joins them before returning the result in the function call.

  type = "text/javascript" > function concatenate ( first , last ) { var full ; full = first + last ; return full ; } function secondFunction () { var result ; result = concatenate ( 'Zara' , 'Ali' ); document . write ( result ); } 
 Click the following button to call the function  type = "button" onclick = " secondFunction () " value = "Call Function" > 
 Use different parameters inside the function and then try. 

Result

Following the above function will produce the result

According to Tutorialspoint

Previous article: Loop control in JavaScript

Next article: Event (Event) in JavaScript

4.5 ★ | 2 Vote