Thinking of a Function… in a new way.
You can think of a JavaScript function as a "black-box". You can put data into it and get results from it. As an example you could create a JavaScript function that would add two numbers and return the result back to another part of your program.
Example…
<SCRIPT> AddThem(2,3); function AddThem(Value1, Value2) { var Result; Result = Value1 + Value2; alert("The sum of " + Value1 + " and " + Value2 + " is " + Result); } </SCRIPT>
Click this button to try the above program.
Note that the function header has two variables between the required parenthesis:function AddThem(Value1, Value2) These are identified as: Value1 and Value2. These can be any legal JavaScript variable identifiers, you can have as many as you wish and they must be separated by commas. In our example, we only use two because we want to pass only two values to this function. This is called the parameter list of the function.
The next thing to note about the above program is that when the function is called: AddThem(2,3);Which contains the values that will be passed to the function definition - In this case 2 and 3. When the function is activated, Value1 is passed the number 2 and Value2 is passed the number 3.function AddThem(Value1, Value2) The body of the function now has two values to add (the 2 and the 3). { var Result; Result = Value1 + Value2;
Copyright © 2000 Adamson House, Ltd.