This example is just like example 2, except it changes the value of the parameter in the function to demonstrate that it has no effect on the value of the argument in the calling routine, since functions work with their own local copy.
// Calling routine
function calcKM()
{
// declarations
var mph = 0;
var kph = 0;
// input
mph = document.getElementById("txtMPH").value;
// processing
kph = calcMphToKph(mph);
// output
document.getElementById("txtKPH").value=kph;
}
// Function to convert miles per hour to kilometers per hour
function calcMphToKph(mph)
{
// declarations
var kph = 0;
const conversionFactor = 1.609344;
// processing
kph = mph * conversionFactor;
kph = Math.round(kph * 100) / 100;
mph = 200; // bogus line added to illustrate safety of value parameters
// output
return kph;
}
The calling routine has a local variable named kph and a parameter named mph.

When calcMphToKph is called, memory is allocated for the parameter mph, the local variable kph, and the local constant conversionFactor.

The parameter mph receives its value from the argument in the calling routine, and conversionFactor is assigned a value.

A value for the local variable kph is calculated by multiplying mph by conversionFactor.

Note that a bogus line was added to reset the mph in calcMphToKph to 200 to illustrate that it has no effect on mph in the calling routine.

When the return kph statement is reached the value is returned through the function call and assigned to the calling routine's local variable kph. The mph in calcKM is still unchanged.

After returning, the memory that was allocated for calcMphToKph is disposed of, and the returned value remains stored in kph.
