Below will teach you about functions
http://javascriptwithshrinivas.blogspot.in/2013/05/about-objects.html
Functions: Functions
are useful to reuse code; we can call n number of times to use code.
copy code from below
<html>
<head>
<title>Functions</title>
<script type="text/javascript">
function meet(who) {
alert("Greetings, " + who);
}
</script>
</head>
<body>
<h1>Function example </h1>
<script type="text/javascript">
meet("Great");
meet("Shrinivas");
</script>
</body>
</html>
---------don't copy this line-End above---------------------------------
Explanation:
copy code from below
<html>
<head>
<title>Functions</title>
<script type="text/javascript">
function meet(who) {
alert("Greetings, " + who);
}
</script>
</head>
<body>
<h1>Function example </h1>
<script type="text/javascript">
meet("Great");
meet("Shrinivas");
</script>
</body>
</html>
---------don't copy this line-End above---------------------------------
Explanation:
meet ("Great"); after calling like this, meet function
pass "Great" string to meet function assign to whom, now alert
display Greetings Great
Next same process repeated display
Greetings Shrinivas
Returning a Value example:
What is difference between above function and Returning a Value example is
Above function will just display message to user and this Returning a value example return a value to script.
Below example Returning a Value example:
<html>
<head>
<title>Functions</title>
<script type="text/javascript">
function Average(a,b,c,d,e) {
result =(a+b+c+d+e)/5;
return result;
}
</script>
</head>
<body>
<h1>Function example </h1>
<script type="text/javascript">
score=Average(25,35,45,55,65);
document.write("The average is:" + score);
</script>
</body>
</html>
Returning a Value example:
What is difference between above function and Returning a Value example is
Above function will just display message to user and this Returning a value example return a value to script.
Below example Returning a Value example:
<html>
<head>
<title>Functions</title>
<script type="text/javascript">
function Average(a,b,c,d,e) {
result =(a+b+c+d+e)/5;
return result;
}
</script>
</head>
<body>
<h1>Function example </h1>
<script type="text/javascript">
score=Average(25,35,45,55,65);
document.write("The average is:" + score);
</script>
</body>
</html>
No comments:
Post a Comment