B.3. Declaring a Method with a Parameter

In our car analogy from Section 1.8, we discussed the fact that pressing a car’s gas pedal sends a message to the car to perform a task—to go faster. But how fast should the car accelerate? As you know, the farther down you press the pedal, the faster the car accelerates. So the message to the car actually includes the task to perform and additional information that helps the car perform the task. This additional information is known as a parameter—the value of the parameter helps the car determine how fast to accelerate. Similarly, a method can require one or more parameters that represent additional information it needs to perform its task. Parameters are defined in a comma-separated parameter list, which is located inside the parentheses that follow the method name. Each parameter must specify a type and a variable name. The parameter list may contain any number of parameters, including none at all. Empty parentheses following the method name (as in Fig. B.1, line 7) indicate that a method does not require any parameters.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset