Functions
Like any other languages, java script can be categorized by functions. Function is
a collection of java script code that performs an specific task and can be used
anywhere in the document by calling it's name. A function can also return a value.
The advantage of it, is to avoid
repeating codes that does same tasks all over your document. Functions are usually
defined in the head of the document and called in the body. Here is a syntax for
none-parameterized function: function functionName()
( java script statement )
Here is a syntax for parameterized function: function
functionName(argument1, argument2, etc) ( java script statement )
A function can return a value.
Function that returns a value must have a return
statement in it. The following function returns the product of two numbers:
function product()
( x=2; y=3; z=x*y;
return z; )
Functions are not executed before they called. When you calling a function, three
things are important.
- The name of the function
- The number and type of parameters the
function expects
- What function is supposed to return; for example, a
number, a string, or whatever
Functions are usually called from the
body section of html document within a java script code.
Here is how you would call a none-parameterized function:
functionName()
And here is how you would call parameterized function: functionName(argument, argument,etc).
The following is complete example of none-parameterized function:
<html> <head> <script
language="javascript"> function calculate()
{ var X = 5;
var Y = 4; var Z= X*Y;
var xval="X has the value: ";
var yval=" Y has the value: ";
document.write(""+xval+""+""+X+""+","+""+yval+""+""+Y+""+","+" X*Y is
equal "+""+Z+""); } </script>
</head> <body> <script
language="javascript"> calculate();
</script> </body> </html>
|
A variable that has a double quoted value is called
string variable because the values are kept as they appear. It's integer variables such
as X & Y above that can be calculated. Document.write has an object 'document'and a
method 'write'. Document defines characteristics of the overall body of a web page and
write method writes the the expression to the specific document object. The function
is called from the body. The result of the above sample is as follows:
X has the value: 5, Y has the value: 4, X*Y is equal 20 |
The following is complete example of parameterized function:
<html> <head> <script language="javascript">
function
names(firstName,lastName) { document.write
(firstName);
document.write(lastName); document.write("<br>"); }
</script> </head> <body> <script
language="javascript"> names("Cilmi","
B.");
names("Warsame"," Dool");
names("H."," Faaruuq");
</script> </body> </html> | When arguments are passed to a function, their values must be
provided when the function is called. This function simply lists the provided values
on the page. We passed two arguments; firstName and lastName to the function. Then
we write the arguments using document.write statement. The function expects value
when it's called. Call without values will display undefined.
The following is result from this script: Cilmi, B. Warsame, Dool H. Faaruuq |
The following is complete example of a parameterized function that returns value:
<html> <head>
<script language="javascript">
function calculateTotal(numOrdered, itemPrice) {
var total=numOrdered*itemPrice;
return total; } </script> </head> <body>
<script language="javascript">
var price=calculateTotal(4,5.99);
document.write("Total: "+price); </script> </body>
</html>
| We passed numOrder and itemPrice to this
function. We created a variable called total which is equal to product of the
two other numbers. Then we returned the total. We then created another variable
called price when the function is called and set it equal to the function name and
argument. This variable simply stores returned value from the function. Then we
write the variable price using document.write statement. The result of this
script is as follows: Total: 23.96 |
Variables & Arrays
If Statement
|