You pass values to a
function by putting the values between the parentheses when you call the
function, as follows:
functionname(value,value,...);
Of course, the
variables can’t just show up. The function must be expecting them. The function
statement includes variables names for the values it’s expecting, as follows:
function
functionname($varname1,$varname2,...)
{
statements
return;
}
For
example, the following function computes the sales tax:
function
compute_salestax($amount,$custState)
{
switch
( $custState )
{
case
“OR” :
$salestaxrate
= 0;
break;
case
“CA” :
$salestaxrate
= 1.0;
break;
default:
$salestaxrate
= .5;
break;
}
$salestax
= $amount * $salestaxrate;
echo
“$salestax<br />”;
}
The first line shows
that the function expects two values — $amount and $custState. When you call
the function, you pass it two values, as follows:
$amount
= 2000.00;
$custState
= “CA”;
compute_salestax($amount,$custState);
In this case, the
amount passed in is 2000.00 and the state is CA. The output is 2000, because
the salestaxrate for CA is 1.0.
No comments:
Post a Comment