What are Methods?
Each method has its own name. When that name is encountered in a program, the execution of the program branches to the body of that method. When the method is finished, execution returns to the area of the program code from which it was called, and the program continues on to the next line of code. Good programmers write in a modular fashion which allows for several programmers to work independently on separate concepts which can be assembled at a later date to create the entire project. The use of methods will be our first step in the direction of modular, top-down programming. Top-down programming is an approach to writing programs that breaks the task of the program down into smaller subtasks. These subtasks can be implemented using methods. Methods are time savers, in that they allow for the repetition of sections of code without retyping the code. In addition, methods can be saved and utilized again and again in newly developed programs. A method consists of a declaration and a body.
You have already been using methods when you use There are two basic types of methods:
How to invoke (call ) a method (method invocation): When a method is invoked (called), a request is made to perform some action, such as setting a value, printing statements, finding a sum, returning an answer, etc. The code to invoke the method contains the name of the method to be executed and any needed data that the receiving method requires (parameters). The required data for a method are specified in the method's parameter list.
FYI: It is possible to code two or more methods with the same name, but with unique combinations of parameters. This process is known as overloading. In such a case, the number of parameters and/or the data types of the parameters, form a designation of the method, where each method's designation must be unique. We will not be addressing overloading of methods at this level.
For those of you who are familiar with other programming languages, Return to Unit Menu | JavaBitsNotebook.com | MathBits.com | Terms of Use | JavaMathBits.com |