Previous Example-|-Next Example-|-Return to Chapter Listing

Example 7.9:
Call By Value

This example is the same as 7-8, except that the function, Percentage, sets both of its arguments (sNum and sPerc), to 0 after it has performed the calculation. This has no effect on the numbers that are displayed and shows that the values passed into a function can be changed inside the function but this has no effect on the values outside of the function. (If you didn't type numbers into the Prompt box, you will have seen an error--we haven't done any error checking in this script.)
Click on these buttons to view the values held by nValue1 and nValue2:

These are the scripts we used. First, in the HEAD: <SCRIPT LANGUAGE="JavaScript"> <!-- function Percentage(nValue1, nValue2) { var nResult = nValue1 * (nValue2/100) nValue1 = 0 nValue2 = 0 return nResult } //--> </SCRIPT> Later in the Web page the function is called by this script: <SCRIPT LANGUAGE="JavaScript"> <!-- var nValue1 = prompt("Type in a number","") var nValue2 = prompt("Type in a the percentage you wish to calculate","") var nPerc = Percentage(nValue1, nValue2) document.write("<H3>" + nValue2 + " percent of " + nValue1 + " = " + nPerc + "</H3>" ) //--> </SCRIPT> And we created these buttons: <FORM> <INPUT TYPE="button" NAME="AlertButton" VALUE="nValue1" onclick="alert(nValue1)"><BR> <INPUT TYPE="button" NAME="AlertButton" VALUE="nValue2" onclick="alert(nValue2)"> </FORM>
Previous Example-|-Next Example-|-Return to Chapter Listing