Level no.24
JAVA-SCRIPT
Lecture #2
Functions in Java-script
Functions are blocks of reusable code in java-script that can be defined and executed when needed. In java script a function is executed when something invokes it means call it.
Function Declaration :
You can declare a function in java-script using "function" keyword, followed by function name and set of parentheses. Function name can contain letter, digits, lowercase, uppercase, underscore and dollar sign, and same rules followed for variables.
Here is the example of function declaration.
Syntax:
function function-name( ) {
// code to be executed
}
Code: (addition of two numbers)
<!DOC TYPE>
<html>
<head>
<title> Java-script </title>
</head>
<body>
enter 1 number: <input type=" text" id="z"> <br> <br>
enter 2 number: <input type=" text" id="x"> <br> <br>
<button onclick= abc( )> Click </button>
Answer is: <input type="text" id="c">
</body>
<script>
function abc( ){
var num1= parseInt ( document.getElementById("z").value)
var num2= parseInt ( document.getElementById("x").value)
var add=num1+num2
document.getElementById("c").value=add
}
</script>
</html>
Well described
ReplyDelete