Time for action – adding code between the parentheses

We're going to modify LearningScript to send some information to the AddTwoNumbers() method to make it much more useful.

Why would we need to send information to a method?

A script may need to add two numbers several times, but they probably won't always be the same two numbers. We could possibly have hundreds of different combinations of "two numbers" to add together. This means that we need to let the method know, which two numbers need to be added together at the moment when we call the method.

Time for action – adding code between the parentheses

Using the preceding screenshot, perform the following steps:

  1. Open LearningScript in MonoDevelop to modify it.
  2. Add lines 6, 7, and 8 to declare three integer variables.
  3. Add lines 22 to 26 to define the AddTwoNumbers() method with parameters.
  4. Add lines 12, 13, and 14 to call the AddTwoNumbers() three times.
  5. Save the file.
  6. Click on Play in Unity.

What just happened?

As this script executes, the AddTwoNumbers() method is called three times on lines 12, 13, and 14. The method's code block adds two numbers and displays the result in the Unity Console (see the yellow arrows in the following screenshot):

What just happened?

Those parentheses are like a cubbyhole. When we call AddTwoNumbers() a couple of numbers are stuffed into the cubbyhole. When the code block executes, it takes those two numbers held in the cubbyhole and uses them on line 24.

There's a special name for that information between the parentheses of a method definition, such as line 22—the code is called the method parameters.

Specifying a method's parameters

If you look up the word parameters in the dictionary, your brain will probably seize up. All it means is that the method has to be able to use the information you send it, so you simply have to specify the type of data the method is allowed to use. That's it, it's very simple.

In the earlier screenshot, on line 22 the red arrows pointed to the type of the declared variables firstNumber and secondNumber. The type is int, or integer. Now notice the red arrow pointing to the variables number1, number2, and number3. They are also of the type int. These variables have to be of type int since they store the numbers that will be added in the method, which the parameters specify will be of type int.

So now go look in the dictionary again. You will probably see the word limit in there somewhere. That's what you did when you specified the type of data, an integer, that the method will use. You set some limits on what's allowed.

Ok, so you're setting parameters, or limits, on the type of data the method can use, but what exactly is a parameter? Well, the first parameter is called firstNumber, and what is firstNumber doing? It's storing a value that will be used in the code block on line 24. What do we call things that store data? That's right, variables. Variables are used everywhere.

Note

Remember, a variable is just a substitute name for the value it actually stores.

As you can see on line 22 of the code block, those variables are being added together.

How many parameters can a method have?

We can have as many as you need to make the method work properly. Whether we write our own custom methods, or you use the methods of the scripting reference, the parameters that are defined are what the method will require to be able to perform its specified task.

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

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