Calling a method

In the earlier screenshot, look at lines 12 and 22. Do you notice anything different? They sure don't look the same, do they? The variable names, which the blue arrows point to, are different.

If you are looking at that code and saying "What the heck?" then don't feel bad. When I was first learning the concept of calling methods, I had one heck of time understanding how the code worked. It is, in fact, very simple, but I fought with this for days before the lights came on. I consulted all the programming books I had, written by all the experts, and not a single one had the decency to explain how the code worked. All those book authors just assumed I'd "get it" because after all, they were experts.

I had to figure it out myself with trial and error testing. After many days, I finally had my "Ah-Ha" moment.

Using arguments in the parentheses of a method

Arguments?? Who dreams up these words? We all know what an argument is. Every one of us has been involved in an argument at some time. Well, someone decided this would be a good word to mean something in programming. Sure enough, look it up in a dictionary and you'll probably see something like this: "A value or address passed to a procedure or function at the time of call."

Yup, that explains it totally, right? Ok, let's really learn what arguments are, and what they do in code. In the previous screenshot, look at line 12:

AddTwoNumbers(number1, number2);

Between the parentheses are the variables, number1 and number2. Those two variables are called the arguments that are being passed to the method. In simple terms, the values stored in these two variables, 2 and 3, are placed in the cubbyhole.

On line 22, the method defines that it takes in two parameters called firstNumber and secondNumber. This means, of course, that somewhere in this process these parameters will have to have values assigned them.

Here's the secret I finally discovered on my own. Behind the scenes, where you can't see, the values 2 and 3, that are in the cubbyhole, are now assigned to the variables firstNumber and secondNumber.

You don't see this code, but if you could see it, what happens with arguments and parameters looks just like this:

firstNumber = number1;
secondNumber = number2;
  • Since the argument number1 contained the value 2, now the parameter firstNumber contains the value 2
  • Since the argument number2 contained the value 3, now the parameter secondNumber contains the value 3

Now the code block is executed and the value 5 is displayed in the Unity Console.

As you can now see, the names of the arguments and the names of the parameters don't need to be the same. They're just names of variables used in different places in your code. They're just substitutes for the actual values each contain, and it's the value that's getting transferred from the method call to the method code block.

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

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