Defining methods
In the previous chapter, we briefly touched on the role methods play in our programs; namely, that they store and execute instructions, just like variables store values. Now, we need to understand the syntax of method declarations and how they drive action and behavior in our classes.
As with variables, method declarations have their basic requirements, which are as follows:
- The type of data that will be returned by the method
- A unique name, starting with a capital letter
- A pair of parentheses following the method name
- A pair of curly brackets marking the method body (where instructions are stored)
Putting all of these rules together, we get a simple method blueprint:
returnType UniqueName()
{
method body
}
Let's break down the default Start()
method in LearningCurve
as a practical example:
void Start()
{
}
In the preceding output, we can see the following:
- The method starts with the...