|
When asking a function to perform an assignment, a function may need to be given the necessary value to perform its job. For example, if a function is supposed to calculate the area of a square, the function may need at least the measurement of the side to proceed. Another function that needs to
calculate the price of an item after applying a discount rate would need the item’s price and the
discount rate; such a function would produce the net price (price after discount).
An item or value that a function needs is called an argument. It can also be called a parameter. An argument for a function is provided between the parentheses for the function that will need it. Providing an argument to a function is referred to as
passing the argument.
To pass an argument to a function, when implementing the function, type a variable name that represents the argument. Here is an example:
function circleArea(radius);
In the body of the function, use the argument any way you see fit. For example, its value can be involved in a calculation. Here is an example of a function that is passed an argument. The function calculates the area of a circle using the radius that is supplied:
<Script Language="JavaScript">
function circleArea(radius)
{
var area;
area = radius * radius * 3.14159;
}
</Script>
|
To call a function that takes an argument, you must supply the argument. If you know the value of the argument, you can provide it:
<Script Language="JavaScript">
function circleArea(radius)
{
var area;
area = radius * radius * 3.14159;
document.write("The area of the circle is ", area);
}
</Script>
<Script Language="JavaScript">
circleArea(20.75)
</Script>
|
You can also provide the argument using the name of the variable that holds the necessary value. The variable that is passed and the argument used by the function don’t need to have the same name but they must
be of the same data type. For example, if a function is using a numeric value
to perform a calculation, make sure that the variable you are providing is holding a numeric value. Here
is an example of passing an argument to a function using the name of a variable:
<Script Language="JavaScript">
function circleArea(radius)
{
var area;
area = radius * radius * 3.14159;
document.write("The area of the circle is ", area);
}
</Script>
<Script Language="JavaScript">
var measure = 45.25;
circleArea(measure );
</Script>
|
A function can also take more than one argument. In this case, provide a name for each argument between the parentheses of the function. The arguments must
be separated by a comma. Here is an example:
function rectangleArea(length, height);
In the body of the function, use the variables in any necessary way. Here is an example:
<Script Language="JavaScript">
function rectangleArea(length, height)
{
var area;
area = length * height * 3.14159;
document.write("The area of the rectangle is ", area);
}
</Script>
<Script Language="JavaScript">
var L, H;
L = 52.05;
H = 46.55;
rectangleArea(L, H);
</Script>
|
The arguments of a function don’t have to be of the same type. One of the arguments could be a
natural number (integer) while another argument could be a string; yet another argument of the same function could be a
decimal number or a character. If you implement a function that takes different types of arguments, when calling the function and supplying the arguments, provide the arguments following the order of the types of arguments supplied. Here is an example of a function that takes different types of arguments:
<Script Language="JavaScript">
function rectangleArea(length, height, shape)
{
var area;
area = length * height * 3.14159;
document.write("The area of the ", Shape, " is ", area);
}
</Script>
<Script Language="JavaScript">
var L, H, S;
L = 52.05;
H = 46.55;
S = "rectangle";
rectangleArea(L, H, S);
</Script>
|
|