function
function is a collection of statements that is executed when it is called at some point in the program.
function enhances the code readability and usability by using it number of times in a program, without need of writing the repeated code.
function are divided into two categories
function is a collection of statements that is executed when it is called at some point in the program.
function enhances the code readability and usability by using it number of times in a program, without need of writing the repeated code.
function are divided into two categories
- function without parameters
- function with parameters
Built-in global function
- alert() - It is used to display alert message such as some error or message. It contains an OK button, which user has to click to continue with the execution of the code.
- confirm() - It is used to display message as well as return true or false value. The confirm box displays a dialog box with two button OK and Cancel. Javascript will be executed on the basis of the choice user takes.
- prompt() - It is used to take input from the user. It contains a text box and OK and Cancel buttons. If the user clicks OK button, the input value is returned. Otherwise, the box returns null.
- eval() - evaluates and executes a string and return null.
- isFinite() - returns a boolean value, true or false, indicating whether the argument passed to it is finite or infinite.
- isNaN() - determines whether or not a value is an illegal number. NaN standa for not a number.
- parseInt() - extracts a number from the beginning of a string. this function parses the string and returns the first integer value found in the string.
- parseFloat() - extracts a number from the beginning of a string. this function parses the string and returns the first floating point value found in the string.
- Number() - converts a value of an object into a number. if the object contains boolean value,then it returns 1 for true and 0 for false.
- escape() - encode the string so that it can be transmitted across any network to any computer that supports ASCII characters. This function encodes special character except *,@,-,_,+ and /.
- unescape() - decodes a string that is encoded by the escape() function.
Defining function
function function_name (parameter1, parameter2,...parameter n)
{
//block of statements
}
Invoking function
function_name(parameter1 value, parameter2 value ... parameter n value);
Return Statement
A return statement specifies the value that is returned from a function.
Syntax
return value;
Example 1
<!DOCTYPE html>
<html>
<head>
<title>javaScript function</title>
</head>
<body>
<h1>JavaScript function</h1>
<script>
var number1=89;
var number2=78;
var number3=67;
function func_Addition(number1, number2, number3) //function definition
{
var sum = number1 + number2 + number3;
document.write("Sum :" + sum);
}
func_Addition(number1, number2, number3); //function invocation
</script>
</body>
</html>
Example 2
<!DOCTYPE html>
<html>
<head>
<title>javaScript function</title>
</head>
<body>
<h1>JavaScript function</h1>
<script>
var number1=89;
var number2=78;
var number3=67;
function func_Addition(number1, number2, number3) // function definition
{
var sum = number1 + number2 + number3;
return sum; // return value
}
var result = func_Addition(number1, number2, number3); //function invocation
document.write("sum :" + result);
</script>
</body>
0 comments:
Post a Comment