JavaScript Functions | Types of JavaScript Functions Tutorial By WDH

JavaScript Functions


JavaScript Functions
A group of statements that is put (or defined) once and then can be used (by reference) repeatedly on a web page. Also known as subprogram, procedure, subroutine.
A function is written as a code block (inside curly {} braces), preceded by the function keyword.
Syntax JS Function 
function function_Name(parameter1, parameter2, parametrn)
{
Some code to executed
}
Example
Output
Advantages of functions
There are some advantages of using functions:
  • Numbers of lines of code is reduced
  • Code becomes to easier to read and understand
  • Code becomes easier to maintain as changes need to be made only at a single location instead multiple locations
Functions Parameters 
  • Facility to pass different parameters while a calling a function
  • Passed parameters can be captured inside the function and any manipulation can be done over those parameters
  • A function can take multiple parameters separated by comma.
Function Return
When JavaScript reaches a return statement, the function will stop executing. If the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking (calling) statement. Functions often compute a return value. The return value is "returned" back to the "caller":
Output