Example Program 4

The next example of a user-defined function allows the function to pass back a value to the calling program.

<html>
<head>
<script type='text/javascript'>

function CalcAreaOfRec(Length, Height)
{ // begin CalcAreaOfRec
var Area;

Area = Length * Height;

return Area ;
} // end CalcAreaOfRec

// Main program
var Length ;
var Height ;
var AreaOfRec ;

Length = prompt('What is the length?');
Height = prompt('What is the height?');

AreaOfRec = CalcAreaOfRec(Length, Height);

alert("The area of this rectangle is " + AreaOfRec);

</script>
</head>
<body>
</body>
</html>

Note: Another name for a parameter is argument.

Example Program 5 (Part 1)