Chapter 4. Getting into the Details of Methods
In the previous chapter, you were introduced to a variable's scope, within which a variable exists and is allowed to be used. The scope is determined by the opening and closing curly braces. The purpose of those curly braces is to act as a container for a block of executable code—a code block. In the second chapter, you understood that a method is a code block that can execute by just calling the method's name. It's time to understand the importance of code blocks and the variables used in them. A method defines a code block that begins and ends with curly braces.
In this chapter, we will cover the following topics:
Using methods in a script
Naming methods the good way
Defining a method
Calling a method
Returning a value from a method
Variables are the first major building block of C# and methods are the second, so let's dive into methods.
Using methods in a script
There are two reasons to use methods in a script:
All of the executable code in a script is inside methods. The first purpose of a method is to work with the member variables of the class. The member variables store data that is needed for a component to give a GameObject
its behavior. The whole reason for writing a script is to make a GameObject
do something interesting. A method is the place where we make a behavior come to life.
The second purpose of a method is to create code blocks that will be used over and over again. You don't want to be writing the same code over and over. Instead, you place the code in a code block and give it a name so that you can call it whenever needed.
Let's take a quick look at this example:
This is a perfect example of the function that does something useful. It might look a bit strange to you as it takes two parameters. Don't worry about it too much as of now; we...
Always use meaningful names for your methods. Just as I explained for variables, if you don't use good names, then six months from now, you will be confused.
Since methods make GameObject
do something useful, you should give your method a name that sounds like an action, for example, JumpOverTheFence
or ClimbTheWall
. You can look at those names and know exactly what the method is going to do.
Don't make them too simple. Suppose you name a method Wiggle
. Sure, you know what Wiggle
means right now, but six months later, you'll look at that and say "Wiggle? Wiggle what?" It takes only a moment more to be a little more precise and write WiggleDogsTail
. Now, when you see this method name, you'll know exactly what it's going to do.
Beginning method names with an uppercase letter
Why? We do this to make it easier to tell the difference between a class or method and a variable. Also, Microsoft recommends beginning method names with an uppercase letter. If someone else ever looks...
Defining a method the right way
Just as with 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," or even "We have to implement a method." Which is correct? In C#, it doesn't make any difference. Use whichever term helps you learn more easily. 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, that a method will return to the place from where it was called
The name of the method should be followed by a pair of parentheses
A pair of curly braces should be present to contain the code block:
Looking at LearningScript
once again, or any Unity-generated script, you can see that the Start()
method has the three minimum...
Understanding parentheses – why are they there?
One thing for sure is that parentheses make it 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 be called multiple times. That's one of the reasons a method is created in the first place—so that we don't have to write the same code over and over. Remember the AddAndPrintTwoNumbers()
example method? We have mentioned that a method can take some input parameters. Why is this useful?
A script may need to add two numbers several times, but they probably won't always be the same two numbers. We can have possibly 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. Let's write a code example to make sure you fully understand it:
Lines 7, 8, and 9 should be quite clear to you—simple declarations of variables.
Let's take...
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 that the method is allowed to use. That's it! It's very simple.
In the earlier screenshot, on line 23, we declared the firstNumber
and secondNumber
variables. The type is int
. Now notice our member variables: number1
, number2
, and number3
. They are also of the int
type. These variables have to be of the int
type since they store the numbers that will be added in the method, which the parameters specify will be of int
the type.
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 that the method will use, an integer in this case. You set some limits on what is allowed.
Okay, so you're setting parameters, or limits, on the type of data the method...
Returning a value from a method
Now it's time to discover the power feature of using a method. This usually means sending data to the method, which you just learned to do. Then we have the method return a value. Previously, we used a void
type method. I have mentioned before that this is a keyword for nothing, which means that the function isn't returning anything.
Let's learn about return
type functions now. We won't use void
anymore. Instead of that, we will write the type of data that we want our method to return. Don't worry if this sounds complicated; it isn't. I remember that, years ago, I had some issues getting my head around it. In practice, this is a very simple concept.
Let's take a look at the following example. I have highlighted two key areas that we will speak about next.
As you can see, this method is very similar to the AddAndPrintTwoNumbers
method that we spoke of previously. The two main differences are highlighted.
A return
type function will always begin with a description...
In this chapter, you learned more details about methods. We will start using methods everywhere in this book. Feel free to come back to this chapter if you feel lost.
In the next chapter, we will introduce a little more complex ideas of handling, lists, arrays, and dictionaries.