Classifications: Depends on taking input parameters and return values, functions classified into four types.
No Parameters & No Return values | With Parameters & No Return values | With Parameters & With Return values | No Parameters & With Return values |
function say() { write(“Hello”); } | function add(a, b) { var c = a+b; write(c); } | function add(a, b) { var c = a+b; return c; } | function getPI() { const PI = 3.142; return PI; } |
No Parameters and No Return values code: this type of function is used to perform operations like clear(), removeAll(), displayAll() etc.
<!doctype html> <html> <head> <script> function sayHello(){ document.write(“Hello All, Welcome”); } </script> </head> <body> <button onclick=”sayHello()”>Say Hello to All</button> </body> </html> |
With Parameters and No return values: This type of function takes input, performs operation and display results from the function itself.
<!doctype html> <html> <head> <script> function add(a, b){ var c = a+b; document.write(“Sum is : ” + c); } </script> </head> <body> <button onclick=”add(10, 20)”>Add</button> </body> </html> |
Function with Parameters and with Return values:
<!doctype html> <html> <head> <script> function add(a, b){ var c = a+b; return “Sum = “+c; } function calculate(){ document.getElementById(“sum”).innerHTML = add(10, 20); } </script> </head> <body> <button onclick=”calculate()”>Add</button> <p id=”sum”> </p> </body> </html> |
Function Without Parameters with Return Values:
<!doctype html> <html> <head> <script> function PI(){ return “PI value is = 3.142”; } function getPI(){ document.getElementById(“pi”).innerHTML = PI(); } </script> </head> <body> <button onclick=”getPI()”>PI Value</button> <p id=”pi”> </p> </body> </html> |