Building a Method

The code we write for a method includes a

  •  method signature defining the method name, parameters, and return type
  • the body implementing the code for the method
  • method return statement.



Here is an example of a method signature.


  • A method signature optionally begins with an accessibility modifier. the default is private.
  • After the accessibility modifier is the return type. this is the type of value returned by the method. void if no return type.
  • Next is the method name, methods names are PascalCase.method name are often verbs or verbs with objects, since the purpose of the method is to perform an action.
  • Last is the parameter list, defining the data that must be passed to this method. this gives the method the data that it needs to perform its operation. For each parameter, the data type and a variable name are defined. the parameters are enclosed in parentheses and separated by commas.
It is always best practice to add XML document comments for the method.



Comments

Popular posts from this blog

Method Overloading