People who have done a little programming have no doubt written a method. Whether it is called a function or a method, is usually based on the language being used. JavaScript uses “function” for its naming convention. A function is essentially a container for lines of code to be executed when the function is called. Functions can be called when a webpage first loads up or as a response to a different page event, such as a button click.
The two places of the HTML page to put JavaScript code is in the “head” section and the “body” section. The best place to put functions in a webpage is in the “head” section, placement here ensures that the code is pre-loaded when the webpage is opened. This avoids any potential conflicts with referencing functions that have not yet been loaded into memory.
A general function would be something like:
function multiply(var a, var b)
{
return a*b;
}
A simple example of a function call. Two numbers are passed to the function and the result is written to the webpage. In most real world cases the number input would be entered by the user.
<html><head>
function multiply(var a, var b)
{
return a*b;
}
</head>
<body>
<script type=”text/javascript”>
var x = 8;
var y = 7;
document.write(multiply(x,y)));
</script>
</body></html>
383e644e-0c86-4f7e-8d19-58962e0bc8b3|0|.0