Game Development Reference
In-Depth Information
Using multiple arguments and parameters
Methods can use more than one argument or parameter. A function definition that uses more than
one parameter looks like this:
bqj_pekjiapdk`J]ia$l]n]iapanKja6Pula(l]n]iapanPsk6Pula%6rke`
w
++`ena_perao
y
The parameters are simply separated by a comma inside the parentheses.
If the function definition uses more than one parameter, the method call also needs to send it the
same number of arguments. Here's what a method call with two arguments might look like:
iapdk`J]ia$]ncqiajpKja(]ncqiajpPsk%7
All you need to do is separate the arguments with a comma. You can create methods with any number
of parameters in this same way.
At some point in your programming career you might need to use a method, but you
won't know how many parameters you'll need. Suppose that you have a method that
needs to track items from trips to the grocery store and you never know how many
items you're going to buy. Today you might have three items, like this:
iapdk`J]ia$epaiKja(epaiPsk(epaiPdnaa%7
Tomorrow you might have five items, like this:
iapdk`J]ia$epaiKja(epaiPsk(epaiPdnaa(epaiBkqn(epaiBera%7
To avoid having to write two different function definitions, you can create one that
stores the arguments it receives in an array. I'll be introducing arrays in Chapter 9, so
you'll probably just want to file this information away for reference later. The function
definition will look like this:
bqj_pekjiapdk`J]ia$***epai=nn]u%6rke`
w
++`ena_perao
y
All the arguments are stored as elements in an array called epai=nn]u. The three dots
in front of the parameter name indicate that the parameter is an array, not a variable.
(For now, you can think of arrays as big filing cabinets that can store lots and lots of
variables.)
 
Search Nedrilad ::




Custom Search