Functions
A function is a group of reusable code which can be called anywhere in your program. This eliminates the need of writing the same code again and again. It helps programmers in writing modular codes. Functions allow a programmer to divide a big program into a number of small and manageable functions.
Like any other advanced programming language, JavaScript also supports all the features necessary to write modular code using functions. You must have seen functions like alert() and write(),We were using these functions again and again, but they had been written in core JavaScript only once.
The most common way to define a function in JavaScript is by using the function keyword, followed by a unique function name, a list of parameters (that might be empty) and a statement block surrounded by curly braces.
Calling a Function
<html>
<head>
<script type="text/javascript">
function sayHello()
{
//alert("Hello JavaScript!");
document.write ("Hello JavaScript!");
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type="button" onclick="sayHello()" value="Say Hello">
</form>
<p>Use different text in write method and then try...</p>
</body>
</html>
Output
Click the following button to call the function
Use different text in write method and then try...
0 comments
Post a Comment