Passing a Value to a Function
You'll often want to take some information and give it to a function to use. This is called passing the information to the function. For example, look at this function definition:
You can have more than one parameter in a function. Just separate them inside the parentheses with commas, like this:
We would like to underscore that parameters are variables, and so they can be passed a variety of different values when the function is called. So these code fragments are all equivalent:
currentScore(6,4); var homeScore = 6; var visitingScore = 4; currentScore(homeScore,visitingScore); currentScore(6,3+1);
In this example, we'll clean up some of the calculations from Script 3.3 by taking them out of the newCard() function, restating them a bit, and putting them into a function with passed values, in order to make it more obvious what's going on. It all happens in Script 3.4.
Script 3.4. By passing values to the setSquare() function, the script becomes easier to read and understand.
To pass a value to a function: