Game Development Reference
In-Depth Information
Using the method parameters
The ^hk_g method is different in one other way from the methods you've written so far: it uses
parameters , which are extra bits of information that a method uses to help it do its job. Parameters
were discussed in Chapter 3, but let's review them again briefly before you continue.
Let's look at a concrete example of how to use parameters. Imagine that you want to write a method
that has the task of displaying the names of fruit. Let's call the method bnqep@eolh]u. You want to be
able to give it the names of any fruit, and the method should then accept those names and display
them in the output window.
The method doesn't know what the names of the fruit will be; you could send it any fruit imaginable. All
it knows is that you'll be giving it two names. You could write some code that looks something like this:
The names of the fruit are provided inside the parentheses that follow the name of the method,
which is known as the argument . You supplied the names of the fruit as strings (words surrounded by
quotes), and separated them with a comma. Now it's the job of the method's function definition to do
something useful with this information.
Here's what the bnqep@eolh]u method's function definition looks like:
If you run this code in the program, the display in the Output panel is the following:
You can change the method call at any time in the program to display different fruit. For example,
you might decide that you're tired of apples and oranges, and write this line of code a little later in
the program:
The Output panel then displays the following:
You didn't change the method in any way; all you did was change the arguments in the method call.
When the arguments are sent to the method's function definition, they're copied into the parameters,
which are highlighted here:
bqj_pekjbnqep@eolh]u$ l]n]iapanKja 6Opnejc( l]n]iapanPsk 6Opnejc%6rke`
Search Nedrilad ::

Custom Search