Functions in JavaScripts,
Functions are made to make life easy. You just make the main function and you call on the function wannever you need it, 1 time,2 times or 100 times.
A function exist out:
- The function keyword
- The name of the function.
- A list of arguments separated by commas and between ()
- The actual function or body of the function, between {}
Example:
<HTML> <HEAD> <SCRIPT language="JavaScript"> <!-- start function square(number) { return number*number } // end --> </SCRIPT> </HEAD> <BODY> <SCRIPT language="JavaScript"> document.write("The function results: ", square(5), "."); </SCRIPT> </BODY> </HTML>The function square uses the argument number
The function is declared in the HEAD and called on in the BODY with argument 5
The write method is used for the output.