♦ JAVASCRIPT Function Definitions
♣ About JAVASCRIPT Function Definitions
A Function is a block of code which runs when associated Function is invoked.A Function can accept parameters and accordingly process the algorithm.
After processing of algorithm, a Function may Return a value(a particular combination of Data Types).
A Function can be of two types in-built and user-defined.
in-built Function comes pre-defined with the JavaScript Language package, for example, concat() is a function for adding two strings together.
A user-defined Function is defined by the user and is customizable.
♣ JAVASCRIPT Function Definitions Syntax
Below is the representation of JAVASCRIPT Function Definition Syntax.Basic Syntax
function function_name(parameters){ //code_block }
Syntax Explanation:
parameters : the user passed parameters which can very the function output code_block : block of code which runs when function is invoked, this also contain return value
♣ Example1: Add Two Numbers in JavaScript
In this example, Two Numbers are added. Example
Input:
Output:
♣ Example2: Recursive Factiorial Calculation in JavaScript
In this example, Factorial of a multiple numbers will be calculated by recursive function. Example
Input:
Output:
♣ Example3: Writing custom print function in JavaScript
In this example, custom function name print is defined instead of the cumbersomely long method "document.write()".Also, println() is defined for printing with the break line.
Example
Input:
Output: