Adscend

Click Here



Click Here

Tuesday

Function Declaration


Functions



1. A function is a block of statements that perform a specific task of same kind.
2. A function has a name and it is reusable.
3. A function invoked from other parts of a program.
4. A function may have return value or not.
5. A function may have parameters or not.
6. The statements inside the function will not be executed automatically.
7. The function is called from any part of the program and then execute the statements inside the function.
8.When a program calls a function, program control is transferred to the called function.
A called function returns control to the caller when its return statement is executed or when function’s ending closing brace is reached.

Syntax:
returnValueType function_name(parameter-list)
{
// statements that will be executed
}

Example: 1
<script type="text/javascript ">
function hello()
{
alert("This text is in function hello");
}
hello();
</script>


Example: 2
<script type="text/javascript ">
function hello()
{
alert("This text is in function hello");
}
hello();
alert("This text is not in function hello");
</script>


Example: 3
<html>
<head>
<script type="text/javascript">
function message()
{
alert("This is my first java script function")
}
</script>
</head>
<body>
<input type="button" value="Click here to display the message"
onclick="message()">
</form>
</body>
</html>
Output



Example: 4
<html>
<head>
<script language="javascript">

function alertMe()
{
alert("hello everybody !")
}
</script>
</head>

<body onload="alertMe()">
</body>
</html>
The browser will show the following :


When browser will open first time, the above dialog box will display.






No comments:

Post a Comment