Need Assistance? 01384 442752 (UK)

What is Function

What is a Function?

When working with Javascript, a function is a piece of code that permits the programmer to perform the same set of commands for as many times as they like without having to copy and paste the code each time. For example, assume you have 15 rectangles on your webpage, each with a different length and width, and you want to be able to calculate the area of each rectangle and display it on the screen. Instead of writing the code that calculates and displays the rectangle area 15 times, this code can be placed in a function and remain dormant until that function is referenced or called upon.

Storing code in a function can also prove to be time efficient, because the developer will be saving time for repetitive tasks by avoiding writing the same code many times in their program. This is known as “code reusability”.

The code inside the functions can be executed in two ways; either by calling the function’s name inside the program, or when an event occurs.

 

If you refer back to lesson 1, you should recall that the JavaScript code can be placed inside an external .js file, and this file should be referenced in each of the HTML pages created. Functions can be created inside the .js file, and therefore can be called by any web page that references the external JavaScript file. This means that the function should only be declared once inside that file, and then called from any other HTML file in the website which allows for code reusability. However, it is also possible to define functions inside the <head> and <body> tags of a document.

To define a function, you need to assign a name for it, a set of parameters, and a return type. (Note that some functions might take zero parameters.)

A function is a piece of code that permits the programmer to perform the same set of commands for as many times as they like without having to copy and paste the code each time. Functions have been used in all programming languages as a great time saver for performing repetitive tasks. For example, assume you have 15 rectangles on your webpage, each with a different length and width, and you want to be able to calculate the area of each rectangle and display it on the screen. Instead of writing the code that calculates and displays the rectangle area 15 times, this code can be placed in a function and remain dormant until that function is referenced or called upon.

Note: There are plenty of pre-defined functions made available as part of the JavaScript language, you will need to know how to call those functions, and how to use them. You won’t be able to modify their code in most of the cases, they are here for you to use as needed. And once you become more familiar with functions. You can start creating your own functions!

Storing code in a function can also prove to be time efficient, because the developer will be saving time for repetitive tasks by avoiding writing the same code many times in their program. This is known as “code reusability”.

The code inside the functions can be executed in two ways: either when the function’s name is called inside the program, or when an event occurs.

To define a function, you need to assign it a name, a set of parameters and a return type. (Note that some functions might take zero parameters.)

In simple words, a function consists of a block of JavaScript code which has a unique name, performs specific tasks and often returns a value. Whenever the name is used in the web application, the corresponding function will be called which means that the function’s JavaScript code will be executed. There are two main purposes for a JavaScript function:

  • The function can be used to perform the same operation for a set number of times without having to copy the same code;
  • A JavaScript function can also link actions on the web page with the JavaScript code, such as text selections, button presses, mouse clicks, and others. This means that user actions can be used to call specific JavaScript functions, which allows the web page to interact with the user and thus be more user-friendly. 

    Study our Javascript Course -See  https://www.acsedu.co.uk/Courses/Information-Technology/Javascript-BIT202-697.aspx 

[26/01/2021 02:13:28]