Chapter 4. Getting into the Details of Methods

In the previous chapter, you were introduced to a variable's scope—where 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 second chapter you saw that a method is a code block that can execute by just calling the method's name. It's time to see the importance of code blocks and the variables used in them. A method defines a code block which begins and ends with curly braces.

In this chapter we will cover the features of methods:

  • Ending method definitions with curly braces
  • Using methods in a script
  • Naming methods properly
  • Defining a method
  • Calling a method
  • Returning a value from a method
  • Using Unity's Update() and Start() methods

Variables are the first major building block of C#, methods are the second, so let's dive into methods.

Ending a method definition using curly braces

At the beginning of Chapter 3, Getting into the Details of Variables you learned about C# statements and the requirement to end them with a semicolon. A method definition has a different requirement.

A method definition ends with a code block between a pair of curly braces. DO NOT end a method definition with a semicolon.

If you do accidentally place a semicolon at the end, MonoDevelop will gladly remind you with an error message that you're not supposed to use a semicolon at the end of a method definition.

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

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