MainMenu

Home Java Overview Maven Tutorials

Saturday, 14 January 2017

Function is Javascript with example

Function is just way to reuse the code which can be called anywhere in the program.
Definition : A javascript function is a block of code designed to perform a perticular task.
Note : To define a javascript function, use the function keyword followed by name & set of paraemeter.
Syntax of function :
<script type = "text/javascript">
function function_name(function parameter)
{
function body
}
</script>
Example : In this example we will call the function from a button
<html>
<head>
<title> Welcome to learing nation</title> <script type = "text/javascript"> function chandan()
{
alert("i have been called");
}
</script>
</head>
<body>
<input type = "button" value = "Submit" onclick = "chandan()">
</body>
</html>
Output : i have been called.
Function Return : Use the return statement to return a value
Example : In this example first function will return the value to the second function :
<html>
<head>
<title> Welcome to learing nation</title>
<script type = "text/javascript">
function chandan(a,b)
{
var c = a * b;
return c;
}
function csc()
{
var x = prompt("Enter your first number");
var y = prompt("Enter your second number");
var z = chandan(x, y);
document.write("your answer is :" +z);
}
</script>
</head>
<body>
<input type = "button" value = "Submit" onclick = "csc()">
</body>
</html>

No comments:

Post a Comment