Defining a method properly

Just like for variables, we have to let Unity know about a method before we can use it.

Depending on who you talk to, some will say we have to declare a method, others will say we have to define a method. Which is correct? In C#, it doesn't make any difference. Use which ever term helps you learn easier. I like to say I'm defining a method's code block, nothing like declaring a simple variable on a one line statement.

The minimum requirements for defining a method

There are three minimum requirements for defining a method:

  • The type of information, or data, a method will return to the place where the method was called
  • The name of the method should be followed by a pair of parentheses
  • A pair of curly braces should be present for containing the code block:
    returnDataType   NameOfTheMethod ( )
    {
    }

Looking at LearningScript once again, or any Unity generated script, the Start() method has the three bare-bone minimum requirements for a method:

void Start ()
{
}

Here's what we have:

  • Our first requirement is the type of data the method will return to the place in the code that called this method. This method isn't returning any value, so instead of specifying an actual type of data, the keyword void is used. This informs Unity that nothing is being returned from the method.
  • Second requirement is the method name which is Start().
  • Last requirement is the curly braces, which contains the code that defines what the method is going to do.

This example fulfills the bare minimum requirements to be a method. However, as you can see, there's no code in the code block, so when Start() is called by Unity, it doesn't do anything at all, but it's still a method. Normally, if we aren't going to use a method by adding code to a skeleton method created by Unity, we can simply remove them from our script. It's normally best to remove unused code after the script is done being written.

Here's what we know about this bare minimum method definition as far as Unity is concerned:

  • There's no public modifier, which means this method is private by default. Therefore, this method cannot be called from other scripts.
  • There's no code in the code block. Therefore, this method doesn't do anything, so it can be removed if we wish.

Understanding parentheses – why are they there?

One thing for sure is that it makes easy to recognize that it's a method, but why are they part of a method's name?

We already know that a method is a code block that is going to get called multiple times. That's one of the reasons why a method created in the first place, so we don't have to write the same code over and over. Remember the AddTwoNumbers () method back in Chapter 2. It was very simple method used to explain the concept of a method and how to call it. Now it's time to take the next step and learn the usefulness of the parentheses.

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

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